当发生错误时,pm2不重新启动worker

我正在使用pm2来pipe理我的nodejs express应用程序(以集群模式运行)中的进程。

我们有2种error handling程序

  • 第一:'uncaughtException'将被处理

    process.on('uncaughtException', function(err){}); 

实际上,我并没有声明这样的处理器,让PM2在这种情况下检测到死亡的工作者,所以自动重新启动死亡的工作者。

  • SECOND:表示error handling程序,我的意思是错误将被转发到expressionerror handling程序,而不是uncaughtException处理程序,像下面的error handling程序

     app.use(function(err, req, res, next) {}) 

我也没有声明这个error handling程序与uncaughtException相同的目的。 但在这种情况下pm2不重新启动节点。

任何关于这个问题的想法? 非常感谢

当使用明确的error handling程序捕获错误或甚至是“uncaughtException”事件时,进程仍在运行,所以pm2不会重新启动它。 如果你想pm2重新启动后,每个exception,我会build议这样的事情:

  process.on('uncaughtException', function(e) { console.log('An error has occured. error is: %s and stack trace is: %s', e, e.stack); console.log("Process will restart now."); process.exit(1); }) 

快递error handling程序也一样。 当我们执行process.exit方法时,进程将会终止,pm2会重新启动它。