start(args) { return Promise.resolve() .then(() => this.processFlags(args)) .then(() => { if (this.flags.instances !== undefined && cluster.isMaster) { return this.startWorkers(this.flags.instances); } return this._run(); }); }
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); } }
listenersAdded = true; if (cluster().isMaster) {
// 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);
((isClusterRequired) => { // if it is a master process then call setting up worker process if (isClusterRequired && cluster.isMaster) { setupWorkerProcesses(); } else { // to setup server configurations and share port address for incoming requests server = app.listen(httpPort, () => { logging.info(`Listening on *:${httpPort}`); }); } })(true);
/* describe('Test process avability', () => { it('It should be defined', (done) => { expect(process.hasOwnProperty('send')).to.deep.equal(true) done() }) }) */ describe('Test that spawned process master', () => { it('It should be master', (done) => { expect(cluster.isMaster).to.deep.equal(true) done() }) })
constructor(config) { super(); if (!cluster.isMaster) { throw new InvalidClusterContextError('Master usage allowed only with master cluster context'); } this._config = _.cloneDeep(config); this._worker = undefined; this._status = WorkerStatuses.INITIALIZED; this._assureStatusSequence = this._assureStatusSequence.bind(this); this._errorHandler = this._errorHandler.bind(this); this._onlineHandler = this._onlineHandler.bind(this); this._exitHandler = this._exitHandler.bind(this); this._messageHandler = this._messageHandler.bind(this); }
startInClusterMode() { const cpuCount = OS.cpus().length; if (cpuCount < 2) { this.startInStandaloneMode(); } else if (cluster.isMaster) { logger.info(`Launching in cluster mode across ${cpuCount} CPUs`); for (let i = 0; i < cpuCount; i += 1) { cluster.fork(); } cluster.on('exit', (worker) => { logger.info(`Worker ${worker.id} exited. Launching again...`); cluster.fork(); }); cluster.on('listening', (worker, address) => { logger.info(`Worker ${worker.id} is now connected to ${address.address || 'localhost'}:${address.port}`); }); } else { this.startInStandaloneMode(); } }
constructor(options) { if (cluster.isMaster) { this._impl = new Master(options); } else { this._impl = new Worker(options); } }
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(); }); } });
constructor(work, options) { super(); if (!work || typeof work !== 'function') { throw new Error('You need to provide a worker function.'); } this.keepAlive = options.keepAlive === undefined ? true : options.keepAlive; this.monitor = options.monitor === undefined ? true : options.monitor; this.work = work.bind(this); this.fork = this.fork.bind(this); this.stop = this.stop.bind(this); if (cluster.isMaster) { cluster.setupMaster({ silent: true }); // Is the process from the master process needs to be piped into the workers. // cluster.fork().process.stdout.pipe(process.stdout); } if (cluster.isWorker) { this.work(); return messageHandler.bind(null, process)(); } return this.start(options); }
super(); if (!cluster.isMaster) { throw new InvalidClusterContextError('Master usage allowed only with master context');
init() { if (cluster.isMaster) { let cpuCore = os.cpus().length for (let i = 0; i < cpuCore; i++) { cluster.fork() } cluster.on('online', (worker) => { if (worker.isConnected()) console.log(`worker is active ${worker.process.pid}`) }) cluster.on('exit', (worker) => { if (worker.isDead()) console.log(`worker is dead ${worker.process.pid}`) cluster.fork() }) } else { //init default route app.use(super.init()) // listenint server port http.createServer(app).listen(process.env.PORT) } }
isMaster() { return cluster.isMaster }
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(); }); } });