Shutdown Fastify graceful asynchronously. By default the fastify close hook is called when SIGINT or SIGTERM was triggered.
- Graceful and debug friendly shutdown
- Flush the fastify logger before process exit to avoid losing logs
- Handlers are called in parallel for faster shutdown
npm install --save fastify-graceful-shutdownfastify.register(require('fastify-graceful-shutdown'))fastify.after(() => {
fastify.gracefulShutdown(async (signal) => {
fastify.log.info('Received signal to shutdown: %s', signal)
await doSomethingAsync()
})
})Fastify >=5
- Don't register signal handlers otherwise except with this plugin.
- Can't be used with a different logger other than Pino because we use the child logger feature to encapsulate the logs.
- Use fastify
onClosehook to release resources in your plugin. - The process will be exited after a certain timeout (Default 10 seconds) to protect against stuck process.