2012-05-23 10:35:00 +00:00
|
|
|
/**
|
|
|
|
* A very basic cluster-based server runner. Restarts failed workers, but does
|
|
|
|
* not much else right now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var cluster = require('cluster');
|
|
|
|
var app = require('./ParserService.js');
|
2012-06-23 15:30:52 +00:00
|
|
|
// Start a few more workers than there are cpus visible to the OS, so that we
|
|
|
|
// get some degree of parallelism even on single-core systems. A single
|
|
|
|
// long-running request would otherwise hold up all concurrent short requests.
|
2012-05-23 14:46:08 +00:00
|
|
|
var numCPUs = require('os').cpus().length + 3;
|
2012-05-23 10:35:00 +00:00
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
// Fork workers.
|
|
|
|
for (var i = 0; i < numCPUs; i++) {
|
|
|
|
cluster.fork();
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.on('death', function(worker) {
|
2012-07-14 23:07:43 +00:00
|
|
|
if(!worker.suicide) {
|
|
|
|
console.log('worker ' + worker.pid + ' died, restarting.');
|
|
|
|
// restart worker
|
|
|
|
cluster.fork();
|
|
|
|
}
|
2012-05-23 10:35:00 +00:00
|
|
|
});
|
2012-07-14 23:07:43 +00:00
|
|
|
process.on('SIGTERM', function() {
|
|
|
|
console.log('master shutting down, killing workers');
|
|
|
|
for(var i = 0; i < workers.length; i++) {
|
|
|
|
console.log('Killing worker ' + i + ' with PID ' + workers[i].pid);
|
|
|
|
// disconnect() doesn't work for some reason
|
|
|
|
//workers[i].disconnect();
|
|
|
|
workers[i].kill('SIGTERM');
|
|
|
|
}
|
|
|
|
console.log('Done killing workers, bye');
|
|
|
|
process.exit(1);
|
|
|
|
} );
|
2012-05-23 10:35:00 +00:00
|
|
|
} else {
|
2012-07-14 23:07:43 +00:00
|
|
|
process.on('SIGTERM', function() {
|
|
|
|
console.log('Worker shutting down');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2012-05-23 10:35:00 +00:00
|
|
|
app.listen(8000);
|
|
|
|
}
|