Closed typpo closed 7 years ago
Looks like server.close()
may be what we want here https://github.com/expressjs/express/issues/1366
it looks like server.close() can accept a callback function:
http://glynnbird.tumblr.com/post/54739664725/graceful-server-shutdown-with-nodejs-and-express
// this function is called when you want the server to die gracefully
// i.e. wait for existing connections
var gracefulShutdown = function() {
console.log("Received kill signal, shutting down gracefully.");
server.close(function() {
console.log("Closed out remaining connections.");
process.exit()
});
// if after
setTimeout(function() {
console.error("Could not close connections in time, forcefully shutting down");
process.exit()
}, 10*1000);
}
// listen for TERM signal .e.g. kill
process.on ('SIGTERM', gracefulShutdown);
// listen for INT signal e.g. Ctrl-C
process.on ('SIGINT', gracefulShutdown);
We don't really have any transactions that need to be protected, but we don't want to drop HTTP requests. Wonder if it's possible to have express finish http responses before exiting the process.
http://pm2.keymetrics.io/docs/usage/signals-clean-restart/