// Expose some worker-specific metric as an example setInterval(() => { c.inc({ code: `worker_${cluster.worker.id}` }); }, 2000);
startNewNode(nodeID) { this.logger.info(`Starting ${nodeID} node...`); const worker = cluster.fork(); worker.nodeID = nodeID; worker.on("message", msg => this.workerMessageHandler(worker, msg)); worker.on("disconnect", () => { const idx = this.nodes.findIndex(node => node.worker == worker); if (idx != -1) { const node = this.nodes[idx]; this.nodes.splice(idx, 1); this.logger.info(`Node ${node.nodeID} stopped.`); this.removeNodeIDFromMetric(node.nodeID); } }); worker.send({ cmd: "start", nodeID }); this.nodes.push({ nodeID, worker }); }
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 start(sqlConfig) { //handle cmd line arguments cmdargs.handle(sqlConfig); if (cluster.isMaster && sqlConfig.useCpuCores > 1) { console.log(`Master ${process.pid} is running`); for (let i = 0; i < numCPUs && i < sqlConfig.useCpuCores; i++) { console.log(`Forking process number ${i}...`); cluster.fork(); } cluster.on("exit", function(worker, code, signal) { console.log( "Worker " + worker.process.pid + " died with code: " + code + ", and signal: " + signal ); console.log("Starting a new worker"); cluster.fork(); }); } else { startXmysql(sqlConfig); } }
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(); } }
// 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);
cluster.on('exit', function (worker, code, signal) { console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal); console.log('Starting a new worker'); worker = cluster.fork(); workerList.push(worker); });
worker.on('message', msg => { if (msg.cmd && msg.cmd == 'notifyRequest') { numReqs++; console.log("numReqs =", numReqs); } });
cluster.on('exit', (worker, code, signal) => { logger.warn('worker %s died (code = %s; signal = %s). restart...', worker.process.pid, code, signal); clearInterval(idCheckInterval); endCheckHealth(); canStartCheck = true; cluster.fork(); });
cluster.on('listening', function(worker, address) { console.log("A worker with #"+worker.id+" is now connected to " + address.address + ":" + address.port); });
/** * Worker Event Handlers **/ cluster.on("exit", function (worker, code, signal) { say("Worker with PID: " + worker.process.pid + ", died with code =>" + code + ", signal => " + signal + " Restarting..."); cluster.fork(); });
cluster.on("fork", worker => { workers.push(worker.id); });