Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
require('os').cpus().forEach(function() {
var worker = cluster.fork();
worker.on('online', function(msg) {
console.log('worker', worker.process.pid, 'online');
workers[worker.process.pid] = worker;
});
});
cluster.on('exit', function(worker, code, signal) {
require('os').cpus().forEach(function() {
var worker = cluster.fork();
worker.on('message', function(msg) {
console.log('worker', worker.pid, 'online');
workers[worker.pid] = worker;
});
});
function fork(wid) {
var w = cluster.fork({WORKER_ID: wid});
w._rc_wid = wid;
w._rc_isReplaced = false;
w.on('message', function(message) {
emit('message', w, message);
});
w.process.on('exit', function() {
utils.removeFrom(workers, w);
deactivate(w);
});
workers.push(w);
return w;
}
var launchWorker_1 = function (i) {
var worker = workers_1[i] = cluster.fork();
initWorkerMsg_1.data.id = i;
worker.on('exit', function () {
if (initWorkerMsg_1.data.restartWorkerOnFail) {
console.log('\x1b[33m%s\x1b[0m', 'Restarting worker on fail ' + initWorkerMsg_1.data.id);
launchWorker_1(i);
}
});
handleChildMessages_1(worker);
worker.send(initWorkerMsg_1);
};
process.on('message', function (message) {
cluster.on('exit', function(worker, code, signal) {
if (worker.suicide !== true && (signal || code !== 0)) {
logStatus(worker, code, signal);
var newWorker = cluster.fork();
var indexOfWorker = workers.indexOf(worker);
if (indexOfWorker >= 0) {
workers.splice(indexOfWorker, 1);
}
log.warn('Web cluster worker respawned with worker id: ' + newWorker.id);
workers.push(newWorker);
initWorker(newWorker);
} else {
logStatus(worker, code, signal);
}
});
forkWorkers(count) {
for (let idx = 0; idx < count; idx++)
cluster.fork();
}
function run(num) {
for (let i = 0; i < num; i++) {
cluster.fork();
}
}
restart() {
if (this.running && Date.now() < this.runUntil) {
cluster.fork();
}
}
function fork(number) {
var numProc = number || require('os').cpus().length;
config.n = numProc;
debug('forking %d workers', numProc);
forkLoopProtect();
for (var i = 0; i < numProc; i++) {
cluster.fork({PWD: config.CWD});
}
}