srvc
Version:
Micro-services framework
34 lines (24 loc) • 923 B
JavaScript
module.exports = function(worker, numWorkers, maxRestarts) {
var cluster = require('cluster')
, Logger = require('./logger');
if (cluster.isMaster) {
// Fork workers
for (var i = 0; i < numWorkers; i++)
forkWorker();
cluster.on('exit', function(worker, code, signal) {
Logger.error('worker ' + worker.process.pid + ' died');
var restartsCount = worker.__cluster__restarts;
if (restartsCount < maxRestarts) {
Logger.info('restarting worker');
forkWorker(restartsCount + 1);
} else
Logger.error(maxRestarts, 'restarts reached, worker not restarted');
});
return cluster.workers;
} else
worker();
function forkWorker(restartsCount) {
var worker = cluster.fork();
worker.__cluster__restarts = restartsCount || 0;
}
};