Skip to content

Instantly share code, notes, and snippets.

@Zyber17
Last active December 22, 2015 04:28
Show Gist options
  • Save Zyber17/6417321 to your computer and use it in GitHub Desktop.
Save Zyber17/6417321 to your computer and use it in GitHub Desktop.
App with setup and clusters
var app, cluster, cpu, cpus, express, http, path, setup;
cluster = require('cluster');
if (cluster.isMaster) {
if (process.env.NODE_ENV === 'setup') {
console.log('Starting setup process.');
express = require('express');
setup = require('./routes/setup');
app = express();
app.configure('setup', function() {
setup(function(resp) {
console.log(resp);
process.kill(process.pid, "SIGTERM");
});
});
} else {
cpus = require('os').cpus().length;
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', function(worker) {
console.log("Worker " + worker.id + " died :(");
cluster.fork();
});
}
}else if (process.env.NODE_ENV !== 'setup') {
// Stuff your app does here
}else {
console.log("Uhh. This should never happen. See app.js.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment