How to get specific node PID in Node-Red? - node.js

I am using Node-Red V2.2.2. I would like to restart an specific node of the flow after an error is triggered in it.
I have managed to restart the full flow getting node-red process id. After modifying: settings.js in my .node-red folder:
functionGlobalContext: {
// os:require('os'),
'pid': process.pid
},
I am able to get general process pid from a function node:
var General_pid = context.global.pid
And kill and restart the global process from an Exec node sending General_pid in msg.payload :
Being comando.sh:
#!/bin/bash
taskkill //PID $1 //F
sleep 4
node-red
But i am unable to do this with specific nodes inside the node-red flow.
Almost all info i have searched relied on Status node to get node specific pid,
but in my case, this is the Status node structure (no PID in there):
I have also tried to get PID based on status.source.id using:
RED.nodes.getNode(id);
But RED.nodes is undefined (altough RED is defined, but it only shows functions on print)
Any idea on how to be able to get the node PID to kill it and restart it? I could do it from an Exec node, but if there is an easier way even better.

You don't.
Nodes are not separate processes that can be restarted independently of Node-RED. (While some nodes may fork a new process, e.g. a python script, Node-RED has no access to this and it is all handled inside the node in question)
You have 2 choices:
You can trigger a restart of the deployed flow by making a HTTP call to the /flows Admin API with the header set to reload. Assuming the node with the failure is well written then it should restart cleanly.
Restart all of Node-RED as you are already

Related

How to use pm2 with a nodejs app that uses readline for taking command line input?

I have a Node.js app that uses the node's native readline to be able to take command-line inputs.
When launching the app with pm2, the command-line input is unavailable.
Any ideas how to solve this issue? Other than using systemd and creating an init script myself?
Use pm2 to attach to your process and you will see readline, clearline and cursorTo working as expected.
First get your process id with:
$ pm2 id {your-process-name}
[ 7 ]
Let's say it's 7:
$ pm2 attach 7
if you check the pm2 website they clearly mention the following line: Advanced, production process manager for Node.js. So using it in this context is unnecessary as all pm2 does is start your 'node' process and allows you to manage it, the simple way is to use command line args while starting the process.
for example:
I myself use commander for this purpose. it manages all my command line arguments (u can see its usage). and with pm2 i use it like following:
pm2 start server.js --name production -- --env dev -p 3458
notice -- before --env, it is used to separate pm2 arguments from the arguments you want to supply to your process
p.s.
PM2 has more complex usage than this, in the terms of process management, i myself use it for production level deployment. If you want to take input from a user every time s/he starts your app, then you should stick with using node command only

process.send() from a child_process launched by exec()

I'm launching a Nightmare JS process from another Node process. Normally, I'd launch Nightmare using child_process.fork('./nightmare_script.js'), and that allows the child process to send messages to the parent via process.send().
However, when running it on an AWS Linux machine, it runs headless, so the script is launched as an argument to framebuffer, and I have to spawn the child process using exec('xvfb-run -a --server-args="-screen 0 1024x768x24" node nightmare_script.js'). However, the process.send() calls from the child don't (can't?) make it back to the parent. Is there a way to make that happen?

How can I get source code of nodejs from running app

I have accidentally delete source code of nodejs application, but this application is running, so how can I get source code back from running app?
I hope source code has been cached in some directory.
I was able to recover the full file by attaching the debugger (as TGrif suggested).
To actually recover the code:
Use setBreakpoint('app.js', 10), where 10 is a line of the code you know will be ran over again in the running process
Say pause, then next until it's paused on the script you want to recover.
Finally, say list(5000), where 5000 is an arbitrarily long number of lines to list.
You will now have your full script printed out, albeit with line numbers at the front, but you can use a site like this to remove them.
Hope this helps anyone who encounters this unique issue in the future, as this took me a couple hours to figure out.
There is maybe a way to retrieve some of your source code with the Nodejs debugger.
Assuming Linux OS, you need to get the process id of your application:
$ ps -e | grep node
Next you entering your app in debug mode with something like that:
$ kill -s USR1 PID
where PID is the pid of your node app.
Then your start the debug console:
$ node debug -p PID
If you have an app console, you'll see:
Starting debugger agent.
Debugger listening on port 5858
In your debug console you should see a debug prompt and you can get available commands with:
debug> help
I am able to show some of the running app source with the list command:
debug> list(NUMBER_OF_LINE)
where NUMBER_OF_LINE is the number of source code line you want to display.
I'm not sure this is a one shot try for you or not because my source code was not deleted.
Hope you can get some results.

How can I spawn a new process and quit from a node.js script?

I have a nodeJS command line script which interacts with a website using phantomJS via casperJS/spookyJS. A redirect event is stopping my process from working correctly. How would I respond to the redirect, spawn a new process and kill the process which has been redirected?
e.g:
spooky.on('navigation.requested', function(url, navigationType, navigationLocked, isMainFrame){
if (url.toString().match(/(http:\/\/url(.|\n)to(.|\n)match\/client\/authentication\/login)/i)) {
utils.log('redirect happened');
//get my own process id, launch a new process, get that id.
//kill this id?
}
});
How would I:
Get the process Id of the currently running node process (the one
running the above code - process A)?
Spawn a new node process (process B) which would be independent of
process A
Kill Process A
Process B should continue to run even when process A has terminated. Would I need a third node process (C) to manage process A and B?
use 'detached' option in child_process.spawn() You may also want to detach tty from process (use nohup utility)

node.js main process killed by ABRT signal, but not caught by process.on("SIGABRT",...)

I'm using upstart to launch a daemon to run a node.js (using express and connect-redis) http and https server (in the same node .js file) per
http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/
Everything is fine at startup and all app functionality appear to work as expected, but consistently, after varying duration (sometimes after a few hours, sometimes after a day), the daemon process gets killed and the only log I have of it is found in
daemon.log:
myserver init: myupstartscript main process (3410) killed by ABRT signal
in my node js file, I've placed:
process.on('uncaughtException',...);
process.on('SIGABRT', .... );
process.on('ABRT',...);
none of which catch the event.
I don't know how to simulate the event.
When I tried using
kill -SIGABRT [the pid]
kill -ABRT [the pid]
my process.on('SIGABRT',..) catches those.
Other things I've noticed:
If I run the service WITHOUT the https server, the crash never happens.
Based on my other logs, the crash does NOT result from a user triggered event
None of the other app specific services I'm relying on (redis-server, mongod) appear to be related to the event, they continue servicing as normal.
I've set the upstart script to respawn upon crash, and it does so.
Any help on how I might trace it?
My setup:
Linux iLV1 2.6.35.4-rscloud #8 SMP Mon Sep 20 15:54:33 UTC 2010 x86_64 GNU/Linux
node v0.5.11-pre
Thanks.
If you want to catch node on exit use
process.on('exit', function () {
console.log('About to exit.');
});

Resources