// Expose some worker-specific metric as an example setInterval(() => { c.inc({ code: `worker_${cluster.worker.id}` }); }, 2000);
cluster.on("fork", worker => { workers.push(worker.id); });
/** * Start Moleculer broker */ startBroker() { this.worker = cluster.worker; if (this.worker) { Object.assign(this.config, { nodeID: (this.config.nodeID || utils.getNodeID()) + "-" + this.worker.id }); } // Create service broker this.broker = new ServiceBroker(Object.assign({}, this.config)); this.broker.runner = this; this.loadServices(); if (this.watchFolders.length > 0) this.broker.runner.folders = this.watchFolders; return this.broker.start() .then(() => { if (this.flags.repl && (!this.worker || this.worker.id === 1)) this.broker.repl(); return this.broker; }); }
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; } }
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); }); }
// Worker processes have a http server. http.Server((req, res) => { var pathname = url.parse(req.url).pathname; console.log("\nRequest for " + pathname + " received."); console.log(`Request method: ${req.method}`); // console.log(`Headers ${JSON.stringify(req.headers)}`); res.writeHead(200); res.end(`hello world from worker ${cluster.worker.id} \n`); // notify master about the request process.send({ cmd: 'notifyRequest', workerId:cluster.worker.id, workerPid: process.pid, isMasterProcess: cluster.isMaster}); }).listen(8080);
/** * @returns {number|undefined} */ getId() { return this._worker && this._worker.id; }
cluster.on("exit", function(worker, code, signal){ if((code !== 0) && !worker.exitedAfterDisconnect){ console.warn("Worker " + worker.id + " crashed. Starting a new worker..."); cluster.fork(); refresh_pool(); } });
cluster.on('listening', function(worker, address) { console.log("A worker with #"+worker.id+" is now connected to " + address.address + ":" + address.port); });
//If a worker dies, restart a new one cluster.on('exit', function(w){ console.log('Worker %d died, spinning up a new one.', w.id); cluster.fork(); });
cluster.on("fork", worker => { workers.push(worker.id); });