Reputation: 119
In my setup, I am running node js, behind haproxy, using haproxy as a reverse proxy and load balancer, which will route the calls, to the multiple node servers I have running. On each node server I am running multiple independent node processes as below:
PORT=3000 forever start app1/app.js
PORT=3001 forever start app2/app.js
PORT=3002 forever start app3/app.js
etc....
Right now, on each server I am installing multiple instances of my app and starting these processes manually using forever. My question is, how do I automate the starting of these processes, given that they can vary on each server, because I will run (numOfCpus -1 ) processes on each server ?
Also, if I want to run say 10 nodejs processes of the same app on server 1, would I need to install the app in 10 separate folders, or can I just install it in one folder, since its the same code, and somehow have 10 separate instances of it run ?
I am shocked that this is such a fundamental way of running node in production, if you do not want to use cluster mode, yet there is no documentation or material on how to achieve this.
Upvotes: 1
Views: 4483
Reputation: 7343
One possible way would be fork child processes that is detached from main process so, that child process does not die with the main process.
In the main process, you could write code to start multiple children processes on different ports and then additionally write code to monitor health of running processes by using OS process commands like ps
. you can analyse the source code of vtop
npm which uses these commands to shows stats of running process and CPU usage. Also, you can optimise the code of main process so, that it does not un-necessarily starts the children processes, if they were kicked off by an another main process.
you could do something like below on setTimeInterval
(which vtop npm does) :
child_process.exec('ps -ewwwo %cpu,%mem,comm', function (error, stdout, stderr) {
//parse stdout to extract health of your process
});
Code snippet to fork detached child process:
var spawn = require('child_process').spawn;
var child = spawn('forever', ['start', 'app.js'], {
detached: true,
stdio: [ 'ignore', out, err ]
});
child.unref();
Upvotes: 1