【问题标题】:do you need to explicitly enable clustering when using pm2?使用 pm2 时是否需要显式启用集群?
【发布时间】:2014-12-31 04:38:13
【问题描述】:

我们有以下使用 PM2 之前遗留下来的 sn-p 代码:

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,我们不再需要在代码中进行此检查?

【问题讨论】:

    标签: node.js pm2


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2017-10-08
      • 1970-01-01
      相关资源
      最近更新 更多