Skip to content

Instantly share code, notes, and snippets.

@heymichaelp
Created April 30, 2014 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heymichaelp/11440077 to your computer and use it in GitHub Desktop.
Save heymichaelp/11440077 to your computer and use it in GitHub Desktop.
// determine if the boot command targets development mode
function isDevelopmentMode() {
return process.argv.indexOf( '--development' ) !== -1;
}
// if development mode is targeted, load .env config variables
if ( isDevelopmentMode() ) {
var DotEnv = require('dotenv-node');
new DotEnv();
var EnvBang = require('envbang-node');
new EnvBang();
}
// Cluster Initialization
var cluster = require('cluster');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
require('./app');
console.log('Worker ' + cluster.worker.id + ' running!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment