Skip to content

Instantly share code, notes, and snippets.

@neurotech
Created October 15, 2015 04:12
Show Gist options
  • Save neurotech/282f634a508773d16c9e to your computer and use it in GitHub Desktop.
Save neurotech/282f634a508773d16c9e to your computer and use it in GitHub Desktop.
Graceful shutdown in node
var shutDown = function() {
logger.info('Gracefully shutting down.');
process.nextTick(function() {
logger.info('Shutting down Express.');
server.close();
});
process.nextTick(function() {
logger.info('Shutting down database connection.');
db.close();
});
setTimeout(function() {
logger.error("Couldn\'t shut down gracefully. Forcefully shutting down!");
process.exit();
}, 15*1000);
};
process.on("SIGTERM", shutDown);
process.on("SIGINT", shutDown);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment