使用pm2时是否需要显式启用群集?

时间:2014-12-23 19:25:31

标签: node.js pm2

在我们使用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如何处理集群对象吗?显然,master在pm2守护进程中处理正确,我们不再需要在代码中进行检查了吗?

1 个答案:

答案 0 :(得分:1)

群集发生在您的应用程序之外(在pm2之内),因此无需在其中包含群集模块。