Skip to content

Instantly share code, notes, and snippets.

@A
Created December 12, 2013 07:22
Show Gist options
  • Save A/7924314 to your computer and use it in GitHub Desktop.
Save A/7924314 to your computer and use it in GitHub Desktop.
Daemonize and clusterize express.js app.
#!/usr/bin/env node
'use strict';
/**
* bin/node-simple-http-daemon
*/
// Everything above this line will be executed twice
require('daemon')();
var cluster = require('cluster');
// Number of CPUs
var numCPUs = require('os').cpus().length;
/**
* Creates a new worker when running as cluster master.
* Runs the HTTP server otherwise.
*/
function createWorker() {
if (cluster.isMaster) {
// Fork a worker if running as cluster master
var child = cluster.fork();
// Respawn the child process after exit
// (ex. in case of an uncaught exception)
child.on('exit', function (code, signal) {
createWorker();
});
} else {
// Run the HTTP server if running as worker
require('../app');
}
}
/**
* Creates the specified number of workers.
* @param {Number} n Number of workers to create.
*/
function createWorkers(n) {
while (n-- > 0) {
createWorker();
}
}
/**
* Kills all workers with the given signal.
* Also removes all event listeners from workers before sending the signal
* to prevent respawning.
* @param {Number} signal
*/
function killAllWorkers(signal) {
var uniqueID,
worker;
for (uniqueID in cluster.workers) {
if (cluster.workers.hasOwnProperty(uniqueID)) {
worker = cluster.workers[uniqueID];
worker.removeAllListeners();
worker.process.kill(signal);
}
}
}
/**
* Restarts the workers.
*/
process.on('SIGHUP', function () {
killAllWorkers('SIGTERM');
createWorkers(numCPUs * 2);
});
/**
* Gracefully Shuts down the workers.
*/
process.on('SIGTERM', function () {
killAllWorkers('SIGTERM');
});
// Create two children for each CPU
createWorkers(numCPUs * 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment