这个 NodeJS 集群主机会崩溃吗?

Can this NodeJS cluster master crash?

我们一直在使用 forever 来启动我们的 Express 应用程序,它使用 cluster 来派生工作人员来完成所有工作。也就是说高手只有运行这个代码:

if (cluster.isMaster) {

    // ...
    // ... check arguments and do some initial prep 
    // ...

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        cluster.fork();
    }

    // Listen for dying workers
    cluster.on('exit', function (worker) {
        if (!worker.suicide) {
            // Replace the dead worker if not a startup error like port in use.
            console.log('Worker ' + worker.id + ' died. Replacing it.');
            cluster.fork();
        }
    });
    return;
}

既然 master 没有真正做任何事情,是否需要 "protect" 它与 foreversupervisor 等?有没有什么情况下 master 可能会崩溃并且自动重启它会有价值?

在我看来,没有必要监控集群中的master进程,因为你无法处理SIGKILL

参考PM2 implementation as below,其实是master

var cluster = require('cluster');  
var http    = require('http');  
var os      = require('os');

var numCPUs = os.cpus().length;

if (cluster.isMaster) {  
  // Master:
  // Let's fork as many workers as you have CPU cores

  for (var i = 0; i < numCPUs; ++i) {
    cluster.fork();
  }
} else {
  // Worker:
  // Let's spawn a HTTP server
  // (Workers can share any TCP connection.
  //  In this case its a HTTP server)

  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world");
  }).listen(8080);
}