如果发生不可恢复的错误,如何在 Heroku 中强制重启 NodeJS 进程?

How to force restart NodeJS process in Heroku in case of unrecoverable error?

其中一个第 3 方库很少出现异常行为,并使应用程序进入某种无法恢复的怪异状态。所以我做了一个逃生舱口来捕获错误,然后我调用 process.exit(0) 希望 Heroku 能重新启动它。

今天是第一次出现这种情况,不幸的是,Heroku 花了大约 10 分钟才决定重启测功机。它一直在发送 error code=H10 desc="App crashed" 垃圾邮件。

有什么更好的方法可以让 Heroku 立即重启?我想避免调用 Heroku API,这是一种奇怪的解决方法。

首先,我认为您的“逃生舱口”不是处理节点应用程序“怪异状态”的好习惯。您应该解决问题 heads-on,也许删除该第 3 方库并确保您的应用程序始终按预期工作。

也就是说,“Heroku 花了大约 10 分钟”这一事实是因为您可能正在使用他们的 'Common Runtime'。如果您希望您的服务在失败时立即重新启动,您将不得不在 'Private Spaces Runtime'.

中查看 运行 它们

来自the docs

The Common Runtime implements an incremental backoff policy for crashing dynos

The Private Spaces Runtime does not have a backoff policy. When a dyno crashes it will be continuously restarted with no cool-off period.

你可以使用 Heroku api,这样你就不会得到这个“App crashed”错误: https://devcenter.heroku.com/articles/platform-api-reference#dyno-restart

别忘了使用 Authorization header: https://devcenter.heroku.com/articles/platform-api-reference#authentication