正确停止,等待并重新设置node.js heroku进程 [英] Properly stopping, waiting, and reseting a node.js heroku process
问题描述
我还没有部署,但我不知道该怎么做。
我有一个应用程序使用了大量的后台进程。也就是说,即使在发送响应之后,仍然有与在后台执行的响应相关的功能。因此,我想要做这样的事情:
var server = http.createServer(app).listen(80)
process.on('SIGINT',function(){
server.close()
setTimeout(function(){
process.exit()
},30000)//等待30秒后退出
})
我不是确定这是否正确。更多假设: 谢谢
process
发送给node.js的信号是什么?我必须以不同的方式处理它们吗?
uncaughtException
吗?
ul>
明白了。 Heroku在关机时发送 SIGTERM
信号。如果进程在10秒内没有退出,那么它会发送一个 SIGKILL
信号。因此,以下就足够了:
process.on('SIGTERM',server.close.bind(server))
https://devcenter.heroku.com/articles/dynos#graceful-shutdown-with-sigterm
假设10秒就够了以完成后台流程。
基本上,在退出信号前x秒发送一个关闭信号,您应该很好。
I haven't deployed yet, but I'm not sure how to do this.
I have an app that uses a lot of background processes. That is, even after a response is sent, there are still functions associated with that response executing in the background. Thus, I want to do something like this:
var server = http.createServer(app).listen(80)
process.on('SIGINT', function () {
server.close()
setTimeout(function () {
process.exit()
}, 30000) // Wait 30 seconds before exiting
})
I'm not sure if this is correct or not. More assumptions:
- These background processes are crucial. However, they'll probably take 1-2 seconds, not 30 seconds. Still, I want to put a 30 second delay just for safety.
- This should work for both restarting the process (say, with forever) and stopping the process
- What signals does Heroku (or any other process) send to node.js through
process
? Would I have to handle them any differently? - Would I handle
uncaughtException
any differently?
Thanks
Got it. Heroku sends a SIGTERM
signal when shutting down. If the process doesn't exit in 10 seconds, then it sends a SIGKILL
signal. Thus, the following is sufficient:
process.on('SIGTERM', server.close.bind(server))
https://devcenter.heroku.com/articles/dynos#graceful-shutdown-with-sigterm
Assuming 10 seconds is enough for the background processes to complete.
Basically, send a "close" signal x seconds before a "exit" signal and you should be good.
这篇关于正确停止,等待并重新设置node.js heroku进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!