Created
January 20, 2012 22:34
-
-
Save tylerflint/1650007 to your computer and use it in GitHub Desktop.
node.js 0.6.* cluster raw implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cluster = require("cluster"); | |
var numCPUs = require('os').cpus().length; | |
var children = []; | |
var murderedChildren = []; | |
if (cluster.isMaster) { | |
log.warn("Master: " + process.pid); | |
for (var i = 0; i < numCPUs; i++) { | |
var spawn = cluster.fork(); | |
// add to the list so we can reap children later | |
children.push(spawn); | |
spawn.on('message', function(msg) { | |
log.warn(msg); | |
}); | |
} | |
cluster.on("death", function(worker){ | |
// find out if this child was murdered | |
var wasMurdered = false; | |
for (child in murderedChildren) { | |
if (children[child].pid == worker.pid) { | |
wasMurdered = true; | |
// remove the child from the murdered list | |
// so we don't have a memory leak | |
murderedChildren.splice(child, 1); | |
} | |
} | |
// if not, we want to respawn | |
if (!wasMurdered) { | |
// fork a new worker | |
log.error("restarting worker") | |
var spawn = cluster.fork(); | |
// now add the child | |
children.push(spawn); | |
worker.on('message', function(msg) { | |
log.error(msg); | |
}); | |
} | |
// remove the child from the children list | |
// so we don't have a memory leak | |
for (child in children) { | |
if (children[child].pid == worker.pid) { | |
children.splice(child, 1); | |
} | |
} | |
// kill ourself if all the children were murdered | |
if (children.length == 0) { | |
process.exit(); | |
} | |
}); | |
var deathSigs = ['SIGINT', 'SIGTERM', 'SIGQUIT']; | |
for (sig in deathSigs) { | |
process.on(deathSigs[sig], function() { | |
for (child in children) { | |
// add child to the murdered list | |
murderedChildren.push(children[child]); | |
// relay the kill signal | |
children[child].kill(deathSigs[sig]); | |
} | |
}) | |
} | |
// hot reload | |
process.on('SIGUSR2', function(){ | |
// dup the children array so that we don't try | |
// to kill replacement children after the new | |
// children are up and ready | |
var deathRow = children.slice(); | |
// let's spawn all new children | |
for (var i = 0; i < numCPUs; i++) { | |
var spawn = cluster.fork(); | |
// add to the list so we can reap children later | |
children.push(spawn); | |
spawn.on('message', function(msg) { | |
log.error(msg); | |
}); | |
} | |
// now send a quit signal to all of | |
// the prior children | |
for (child in deathRow) { | |
// add child to the murdered list | |
murderedChildren.push(deathRow[child]); | |
// relay the kill signal | |
deathRow[child].kill('SIGQUIT'); | |
} | |
}); | |
} else { | |
require('./worker'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment