Skip to content

Instantly share code, notes, and snippets.

@flesch
Last active January 2, 2016 03:38
Show Gist options
  • Save flesch/8244822 to your computer and use it in GitHub Desktop.
Save flesch/8244822 to your computer and use it in GitHub Desktop.
eval $([ -f .env ] && cat .env) node cluster.js
var cluster = require('cluster')
, express = require('express')
, app = express()
, cpus = require('os').cpus().length;
app.get('/', function(req, res, next){
res.json({ "env":process.env.NODE_ENV || "development", "pid":cluster.worker.process.pid });
});
if (cluster.isMaster) {
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker %s died', worker.process.pid);
cluster.fork();
});
} else {
app.listen(process.env.PORT || 3000, function(){
console.log("port: %s | pid: %s", process.env.PORT || 3000, cluster.worker.process.pid);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment