Node.JS 关闭挂钩
是否可以拦截默认的 kill
信号并将其用作正常关闭的命令?这是针对 Solaris SMF 的。我发现获得可停止服务的最简单方法是将 :kill
设置为关闭脚本,然后在 Java 中添加关闭钩子。在本例中,我想为 Node.JS 执行此操作。我该怎么做呢?
编辑:目的是
- 停止接收新请求。
- 给现有回调几秒钟的时间来完成。
- 向 stderr 写入一些信息。
@alienhard 的第一个建议是使用 process.on('exit'... 但似乎我无法用这种方法完成第二个任务。
Is it possible to intercept the default kill
signal and use it as a command for a graceful shutdown? This is for Solaris SMF. The easiest way to have a stoppable service that I have found is to set :kill
as the shutdown script and then to add a shutdown hook in Java. In this case, I want to do it for Node.JS. How should I do it?
Edit: The purpose is to
- Stop receiving new requests.
- Give existing callbacks a few seconds to finish.
- Write some information to stderr.
@alienhard's first suggestion was to use process.on('exit'...
but it seems that I would not be able to accomplish number 2 with this method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个
exit
事件:http:// nodejs.org/docs/v0.3.1/api/process.html#event_exit_编辑: 另一种可能对您有用的替代方案是发送类似 SIGUSR1 的信号,而不是终止进程(
kill -s SIGUSR1
),然后监听此信号(请参阅另一个答案中 @masylum 发布的链接),并在完成后或经过一段时间后使用 process.exit() 显式终止。There is an
exit
event: http://nodejs.org/docs/v0.3.1/api/process.html#event_exit_Edit: An alternative that could work for you, is instead of killing the process, sending a signal like SIGUSR1 (
kill -s SIGUSR1
), and then listening for this signal (see link posted by @masylum in another answer) and after you are done or some time has elapsed explicitly terminate with process.exit().我唯一想到的是使用信号事件。
http://nodejs.org/docs/v0.3.1/api/process .html#signal_Events
The only thing that comes to my mind is using signal events.
http://nodejs.org/docs/v0.3.1/api/process.html#signal_Events