Skip to content

Instantly share code, notes, and snippets.

@krosti
Created September 2, 2015 14:17
Show Gist options
  • Save krosti/1e7baf0cc72c663da2ea to your computer and use it in GitHub Desktop.
Save krosti/1e7baf0cc72c663da2ea to your computer and use it in GitHub Desktop.
ExpressJS Cluster test
var cluster = require('cluster');
if(cluster.isMaster) {
var numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
//one instance here
// app.use(...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment