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
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.
I think that you better use a worker process for doing those background jobs (worker dyno on Heroku).
this both free your web server to serve your users as fast as possible and allow you to have better control when scaling (for example, running 3 web dynos and 2 worker dynos).
you can use Redis's pub/sub functionality to submit jobs to the worker dyno (or use a library to do so, such as http://learnboost.github.com/kue/ ).
this way, when a web dyno restarts/dies/etc.. it doesn't have any effect on pending background jobs and when a worker dyno restarts it'll just consume pending jobs and process them, so you don't loose any jobs because of restarting.