Skip to content

Instantly share code, notes, and snippets.

@cvan
Created August 7, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/4a53a992c283e5944c8b6987e301591f to your computer and use it in GitHub Desktop.
Save cvan/4a53a992c283e5944c8b6987e301591f to your computer and use it in GitHub Desktop.
if (process.env.NODE_ENV === 'development') {
// This helps for nodemon to not be prematurely killed.
// See nodemon's docs: https://github.com/remy/nodemon/blob/master/README.md#controlling-shutdown-of-your-script
// Adapted from source: https://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/
const gracefulShutdown = cb => {
setTimeout(() => {
process.exit(0);
cb();
}, 5000);
};
// Handle SIGUSR2, but only once.
process.once('SIGUSR2', () => {
// Graceful shutdown.
gracefulShutdown(() => {
// Kill ourself with the SIGUSR2 signal again.
process.kill(process.pid, 'SIGUSR2');
});
});
process.on('SIGTERM', () => {
// https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
app.close(() => {
// eslint-disable-next-line no-console
console.log('Server closing. Process terminated.');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment