Node.js: How to create “smooth” restart of launched app?-Collection of common programming errors
I took an approach similar to PartlyCloudy, but just using multiple node threads on the same process, and using the OS as the load balancer instead of a separate one.
So I have code in my “app.coffee” code that’s stolen from:
I only added a “setTimeout”. For a period of 5-20 seconds I may have 16 forks on my 8 cores instead of 8, but that’s better for me than downtime.
var childProcesses, cluster, i, numCPUs, signals, _i;
cluster = require('cluster');
numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
childProcesses = [];
for (i in numCPUs) {
childProcesses[i] = cluster.fork();
}
signals = ["SIGINT", "SIGTERM", "SIGQUIT"];
for (i in signals) {
process.on(signals[i], function() {
// THIS IS WHAT KEEPS IT UP
setTimeout(function() {
var j;
for (j in childProcesses) {
childProcesses[j].kill();
}
process.exit();
}, 20000);
});
}
cluster.on('exit', function(worker, code, signal) {
childProcesses.push(cluster.fork());
});
} else {
app.listen(3000);
}