Node.js 在接收 SIGTERM 时的默认行为是什么?

What is the default behaviour of Node.js on receiving SIGTERM?

我一直在阅读文档,但没有明确说明如果没有注册信号处理程序,Node.js 进程是否立即退出。例如,poll queue 中的待处理任务是否已执行或所有内容都已删除?程序员是否总是必须通过跟踪所有待处理的承诺来显式处理正常关闭?

当没有为SIGTERM添加处理程序时,节点将重置终端并立即退出进程。这是一个“放下所有东西”的出口。甚至比 process.exit which does still allow the exit event to fire:

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

添加信号处理程序时(process.once('SIGTERM'),处理程序代码会安排到下一个事件循环。因此,即使您只是在处理程序中调用 process.exit(),行为也会立即改变。

Promises,或者一般来说 Javascript,还没有内置取消的概念。您可以跟踪 promises,但除非使用底层 API 专门解决取消任务的方法,否则没什么可做的。

您可以 just wait though. In the average web application if you close the web server and close the database connection. Existing connections will complete, no new connections will initiate, eventually the process will exit by itself when there is nothing left on the event loop to run. wtfnode 可以方便地帮助调试最终不会像这样退出的进程。