Skip to content

Instantly share code, notes, and snippets.

@wheller
Forked from jdx/boot.js
Last active April 4, 2017 22:53
Show Gist options
  • Save wheller/e26279698012c3f76fd7 to your computer and use it in GitHub Desktop.
Save wheller/e26279698012c3f76fd7 to your computer and use it in GitHub Desktop.
zero-downtime node.js app runner
// This script will boot app.js with the number of workers
// specified in WORKER_COUNT.
//
// The master will respond to SIGHUP, which will trigger
// restarting all the workers and reloading the app.
var fs = require('fs');
var cluster = require('cluster');
var workerCount = process.env.WORKER_COUNT || require('os').cpus().length || 2;
var script = process.argv[2] || 'index.js';
var pidFile = script+'.PID';
var sigtermPause = 20000;
// Defines what each worker needs to run
// In this case, it's app.js a simple node http app
cluster.setupMaster({ exec: script });
// Gets the count of active workers
function numWorkers() { return Object.keys(cluster.workers).length; }
var stopping = false;
// Forks off the workers unless the server is stopping
function forkNewWorkers() {
if (!stopping) {
for (var i = numWorkers(); i < workerCount; i++) { cluster.fork(); }
}
}
// A list of workers queued for a restart
var workersToStop = [];
// Stops a single worker
// Pause for sigtermPause after disconnect before SIGTERM
function stopWorker(worker) {
console.log('stopping', worker.process.pid);
worker.disconnect();
var killTimer = setTimeout(function() {
worker.kill();
}, sigtermPause);
// Ensure we don't stay up just for this setTimeout
killTimer.unref();
}
// Tell the next worker queued to restart to disconnect
// This will allow the process to finish it's work
// Pause for sigtermPause after disconnect before SIGTERM
function stopNextWorker() {
var i = workersToStop.pop();
var worker = cluster.workers[i];
if (worker) stopWorker(worker);
}
// Stops all the works at once
function stopAllWorkers() {
stopping = true;
console.log('stopping all workers');
for (var id in cluster.workers) {
stopWorker(cluster.workers[id]);
}
}
// Worker is now listening on a port
// Once it is ready, we can signal the next worker to restart
cluster.on('listening', stopNextWorker);
// A worker has disconnected either because the process was killed
// or we are processing the workersToStop array restarting each process
// In either case, we will fork any workers needed
cluster.on('disconnect', function(){
// without a small pause the worker is not given time to
// be removed from the count, so forkNewWorkers gets the count
// including the worker that triggered it
setTimeout(forkNewWorkers,100);
});
// HUP signal sent to the master process to start restarting all the workers sequentially
process.on('SIGHUP', function() {
console.log('restarting all workers');
workersToStop = Object.keys(cluster.workers);
stopNextWorker();
});
// Kill all the workers at once
process.on('SIGTERM', stopAllWorkers);
// Fork off the initial workers
forkNewWorkers();
console.log('app master', process.pid, 'booted');
fs.writeFile(pidFile, process.pid, function(err) {
console.log('app master', process.pid, 'booted');
if(err) {
return console.log(err);
}
return 0;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment