Reputation: 2434
We have the following snippet of code leftover from before we were using PM2:
parentCluster = require('cluster');
numberOfWorkers = process.env.WORKERS || require('os').cpus().length;
if (parentCluster.isMaster) {
logger.info('Starting parent cluster with %s workers', numberOfWorkers);
for (_i = 1; 1 <= numberOfWorkers ? _i <= numberOfWorkers : _i >= numberOfWorkers; 1 <= numberOfWorkers ? _i++ : _i--) {
worker = parentCluster.fork().process;
logger.info('Forked worker %s', worker.pid);
}
parentCluster.on('fork', function(worker) {
return logger.info('forked worker ' + worker.process.pid);
});
parentCluster.on("listening", function(worker, address) {
return logger.info("Worker " + worker.process.pid + " is now connected to " + address.address + ":" + address.port);
});
parentCluster.on('exit', function(worker) {
logger.warn('Worker %s has died. restarting...', worker.process.pid);
return parentCluster.fork();
});
} else {
startServer();
}
However, we are now using PM2 which now handles the clustering. And it appears that isMaster is never true. Can someone clarify how PM2 treats the cluster object? So apparently the master is handled in the pm2 daemon correct and we no longer need this check in the code?
Upvotes: 0
Views: 181
Reputation: 2341
The clustering happens outside your application (within pm2) so there is no need to include the cluster module within.
Upvotes: 1