StayWokeOrg / general-congress-hotline

7 stars 26 forks source link

Add SIGINT handling to support graceful reloads #27

Closed typpo closed 7 years ago

typpo commented 7 years ago

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/

typpo commented 7 years ago

Looks like server.close() may be what we want here https://github.com/expressjs/express/issues/1366

diffalot commented 7 years ago

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);