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:
process
? Would I have to handle them any differently?uncaughtException
any differently?Thanks
1- What you actually want, which is preventing the process to exit, can be handled by handling uncaughtException (documentation) event: var handleRequest = function(req, res) { res. writeHead(200); res1. end('Hello, World!\
One of these is beforeExit , and as its name implies, it is emitted right before a Node process exits. You can provide an event handler which can make asynchronous calls, and the event loop will continue to perform the work until it's all finished.
The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With