function dispatch(type, id, arg) { let selectedID = lastID+1; if (selectedID > numCPUs-1) selectedID = 0; cluster.workers[workers[selectedID]].send(JSON.stringify({ id, type, arg })); lastID = selectedID; }
fork(wrk) { if (this.keepAlive) { const worker = cluster.fork(); log.warn(`Worker ${wrk.process.pid} died. Replacer: ${worker.process.pid}`); } if (!Object.keys(cluster.workers).length) { monitor.stop(); } }
function killForks() { var forksToKill = Math.floor((thresholdCapability - numReqs) / threshold) - 1; // Leave at least one online forksToKill = (forksToKill < 0) ? 0 : forksToKill; if (forksToKill > 0 && Object.size(cluster.workers) > 1) { while(forksToKill > 0) { removeWebWorker(webWorkers[forksToKill]); forksToKill--; } } }
function removeWebWorker(id) { console.log("Killing unnecessary fork #" + id); webWorkers.splice(webWorkers.indexOf(id), 1); thresholdCapability = threshold * webWorkers.length; cluster.workers[id].kill(); }
setInterval(function () { const stats = { nworkers: Object.keys(cluster.workers).length }; fs.writeFile('/tmp/skale/worker-controller-stats', JSON.stringify(stats), function () {}); }, 3000);
function addWebWorker() { //cluster.fork({web: 1}); webWorkers.push(cluster.fork({web: 1}).id); //Update thresholdCapability thresholdCapability = threshold * webWorkers.length; cluster.on('online', function(worker) { cluster.setMaxListeners(0); // https://github.com/joyent/node/issues/5108 cluster.workers[worker.id].on('message', messageHandler); }); }
function refresh_pool(){ const workers = Object.values(cluster.workers); for(let i = 0; i < workers.length; i++){ const worker = workers[i]; pool[worker.id] = worker; } }
stop() { monitor.stop(); this.keepAlive = false; let index = Object.keys(cluster.workers).length; while (index) { if (cluster.workers[index]) { cluster.workers[index].process.kill(); cluster.workers[index].kill(); } index -= 1; } log.debug('The cluster has been terminated.'); this.emit('offline'); setTimeout(process.exit, 200); }
function dispatch(type, id, arg) { let selectedID = lastID+1; if (selectedID > numCPUs-1) selectedID = 0; cluster.workers[workers[selectedID]].send(JSON.stringify({ id, type, arg })); lastID = selectedID; }
fork(wrk) { if (this.keepAlive) { const worker = cluster.fork(); log.warn(`Worker ${wrk.process.pid} died. Replacer: ${worker.process.pid}`); } if (!Object.keys(cluster.workers).length) { monitor.stop(); } }
stop() { monitor.stop(); this.keepAlive = false; let index = Object.keys(cluster.workers).length; while (index) { if (cluster.workers[index]) { cluster.workers[index].process.kill(); cluster.workers[index].kill(); } index -= 1; } log.debug('The cluster has been terminated.'); this.emit('offline'); setTimeout(process.exit, 200); }