0

在使用 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();
}

但是,我们现在使用 PM2 来处理集群。看来 isMaster 从来都不是真的。有人可以澄清 PM2 如何处理集群对象吗?显然,在 pm2 守护进程中正确处理了 master 并且我们不再需要在代码中进行此检查?

4

1 回答 1

1

The clustering happens outside your application (within pm2) so there is no need to include the cluster module within.

于 2014-12-30T20:38:13.787 回答