Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active May 2, 2016 17:10
Show Gist options
  • Save eschwartz/a70e228d607b557c301b to your computer and use it in GitHub Desktop.
Save eschwartz/a70e228d607b557c301b to your computer and use it in GitHub Desktop.
Node cluster
#!/usr/bin/env node
/**
* Usage:
*
* node cluster.js ./worker-script.js
*/
const cluster = require('cluster');
const path = require('path');
function startCluster(runWorker, opts) {
opts = Object.assign({
maxWorkers: 1,
restartTimeout: 1000
});
if (!cluster.isMaster) {
return runWorker();
}
// Fork off worker processes
const targetWorkerCount = Math.min(require('os').cpus().length, opts.maxWorkers);
for (var i = 0; i < targetWorkerCount; i++) {
spawn();
}
// Restart failed workers
cluster.on('exit', (worker) => {
console.error(`Worker with pid ${worker.process.pid} died. Spawning a new process...`);
setTimeout(() => spawn(), opts.restartTimeout);
});
}
function spawn() {
const worker = cluster.fork();
console.log(`Spawning worker with pid: ${worker.process.pid}`);
}
// Handle catastrophic failure
process.on('uncaughtException', (err) => {
console.error(`Cluster failed! ${err.message}`);
console.error(err.stack);
});
startCluster(() => {
const workerPath = path.resolve(process.cwd(), process.argv[2]);
return require(workerPath);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment