I have a application in node.js.
This application is divided in 3 parts:
launcher.js, which start the two other part, and restart them on crash/update after handling cleaning.
app.js, which work on the computer himself.
server.js which is used to access log and different command.
The simplified code for launcher is:
var cluster = require('cluster'),
exec = require('child_process').exec,
server;
if (cluster.isMaster) {
cluster.fork();
server = exec('server.js');
cluster.on('exit', function(worker, code, signal) {
//Clean corrupted data, log crash if neccessary, reload source code for update ...
cluster.fork();
});
server.on('exit', function () {
//Same as for app, with a different handling of signal...
server = exec('node server.js');
});
} else {
var self = require('app.js');
self.start();
}
The good thing with cluster is that they are in the same process as the launcher, so I can handle some error without having to restart the app (just calling the right function inside the app for a "soft reboot" of itself), and keep everything in the same process.
While with exec, I m stuck with restarting the server, sometime without knowing what went wrong, and it mean having a subshell, which I dislike.
Is there a way to fork the cluster, but start a different code?