// Expose some worker-specific metric as an example setInterval(() => { c.inc({ code: `worker_${cluster.worker.id}` }); }, 2000);
/** * 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; }); }
// 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);
process.on('SIGINT', () => { console.log("SIGINT received. Performing clean shutdown..."); // Implicitly calls server.close, then disconnects the IPC channel: cluster.worker.disconnect(); });
strapi.start({}, function (err) { // Log and exit the REPL in case there is an error // while we were trying to start the server. if (err) { strapi.log.error('Could not load the Strapi framework.'); strapi.log.error('Are you using the latest stable version?'); process.exit(1); } // Open the Node.js REPL. if ((cluster.isMaster && _.isEmpty(cluster.workers)) || cluster.worker.id === 1) { const repl = REPL.start(strapi.config.info.name + ' > ' || 'strapi > '); repl.on('exit', function (err) { // Log and exit the REPL in case there is an error // while we were trying to open the REPL. if (err) { strapi.log.error(err); process.exit(1); } strapi.stop(); }); } });
function stop() { shouldStop = true; if (!started) { return; } if (stopping) { return; } stopping = true; console.log('stopping'); if (process.argv[2] === 'listen') { server.close(doStop); } else { doStop(); } function doStop() { if (!process.connected) { process.exit(0); } if (cluster.worker) { cluster.worker.disconnect(); } else { process.disconnect(); } } }
db.connect(function(err) { if (err) { console.log('Unable to connect to database.'); process.exit(1); } else { var port = process.env.PORT || 3000; app.listen(port, function() { console.log('Worker with id: ' + cluster.worker.id + ' on port: ' + port); }); //Connect to the mongo database db.connectNoSQL(function(err){ if (err){ console.log('Unable to connect to mongo database.'); process.exit(1); } }); } });
cluster.worker.disconnect(); }, 1000);
//processing jobs Queue.process('every', function (job, done) { console.log('\nChild Worker %s is Processing job with id %s at %s', cluster.worker.id, job.id, new Date()); done(null, { deliveredAt: new Date() }); });
const clusterId = cluster.isWorker ? cluster.worker.id : ''; const tempName = 'hc_' + os.hostname() + '_' + clusterId + '_' + Math.round(Math.random() * HEALTH_CHECK_KEY_MAX); const tempBuffer = Buffer.from([1, 2, 3, 4, 5]);
process.send(cluster.worker.id); }, 5000);
_start() { if (cluster.isWorker) { cluster.worker.on('disconnect', () => this.stop() .then(() => this._exitProcess(0)));
process.on('message', function (token) { global.__DBtoken = token var ServerWorker = new Server(cluster.worker.id) })
strapi.start({}, function (err) { // Log and exit the REPL in case there is an error // while we were trying to start the server. if (err) { strapi.log.error('Could not load the Strapi framework.'); strapi.log.error('Are you using the latest stable version?'); process.exit(1); } // Open the Node.js REPL. if ((cluster.isMaster && _.isEmpty(cluster.workers)) || cluster.worker.id === 1) { const repl = REPL.start(strapi.config.info.name + ' > ' || 'strapi > '); repl.on('exit', function (err) { // Log and exit the REPL in case there is an error // while we were trying to open the REPL. if (err) { strapi.log.error(err); process.exit(1); } strapi.stop(); }); } });