正确停止,等待并重新设置node.js heroku进程 [英] Properly stopping, waiting, and reseting a node.js heroku process

查看:136
本文介绍了正确停止,等待并重新设置node.js heroku进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有部署,但我不知道该怎么做。



我有一个应用程序使用了大量的后台进程。也就是说,即使在发送响应之后,仍然有与在后台执行的响应相关的功能。因此,我想要做这样的事情:

  var server = http.createServer(app).listen(80)

process.on('SIGINT',function(){
server.close()
setTimeout(function(){
process.exit()
},30000)//等待30秒后退出
})

我不是确定这是否正确。更多假设:


  • 这些后台进程至关重要。但是,他们可能需要1-2秒,而不是30秒。不过,为了安全起见,我想延迟30秒。

  • 这应该适用于重新启动进程(例如永远)并停止进程
  • Heroku(或任何其他进程)通过 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆