Skip to content

Instantly share code, notes, and snippets.

@tommedema
Created May 27, 2012 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommedema/2805210 to your computer and use it in GitHub Desktop.
Save tommedema/2805210 to your computer and use it in GitHub Desktop.
var cluster = require('cluster'),
nrCPUs = require('os').cpus().length;
//when we are the master
if (cluster.isMaster) {
//fork workers (one for each CPU)
for (var i = 0; i < nrCPUs; i++) {
spawnWorker();
}
//respawn on death
cluster.on('death', respawnWorker);
//stop respawning when we are quitting
process.once('exit', function() {
cluster.removeListener('death', respawnWorker);
});
function respawnWorker() {
//respawn worker
spawnWorker();
}
function spawnWorker() {
//fork a worker
var worker = cluster.fork();
//tell worker to quit on exit
process.on('exit', function() {
console.log('I NEVER FIRE'); //why does this never fire?
worker.send('SIGQUIT');
});
}
}
else {
//load the worker logic here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment