在nodejs中重新启动集群工作程序而不是侦听连接

时间:2016-07-05 17:41:33

标签: node.js multithreading

我有一个用例,我需要将一个工作人员专门用于侦听来自AWS sqs队列的传入消息并处理它们,其余工作人员正在侦听所需的连接端口。以下是代码

var cluster = require('cluster');

if (cluster.isMaster) {
  /** Master process */

  /**
   * spawn a worker for each available cpu
   * HACK: Downsizing worker count will not restore tasks that were handled by those extra workers
   */
  var cpuCount = require('os').cpus().length;
  for (var i = 0; i < cpuCount; i += 1) {
    cluster.fork();
  }
  /** re-spawn worker if dead */
  cluster.on('exit', function(worker) {
    console.log('worker-' + worker.id + ' died');
    cluster.fork();
  });

} else if (cluster.worker.id == 1) {
  // Check for incoming messages from an AWS SQS queue
} else {
  //Listen on the port for connections 
}

如果侦听端口连接的工作人员死亡,则会恢复。但是,我无法找到如何恢复正在侦听来自SQS队列的传入消息的工作者的解决方案,如果它因某些错误而死亡。

0 个答案:

没有答案