### Install close-with-grace Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Install the library using npm. ```bash npm i close-with-grace ``` -------------------------------- ### Example with Fastify Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Integrate closeWithGrace with a Fastify application for graceful server shutdown. Logs server closing events using Fastify's logger. ```javascript import fastify from 'fastify' import closeWithGrace from 'close-with-grace' const app = fastify() closeWithGrace(async function ({ signal, err, manual }) { if (err) { app.log.error({ err }, 'server closing with error') } else { app.log.info(`${signal} received, server closing`) } await app.close() }) await app.listen() ``` -------------------------------- ### Basic Usage with Delay Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Use closeWithGrace with a specified delay for graceful shutdown. The default delay is 10000ms. ```javascript const closeWithGrace = require('close-with-grace') // delay is the number of milliseconds for the graceful close to // finish. closeWithGrace({ delay: 500 }, async function ({ signal, err, manual }) { if (err) { console.error(err) } await closeYourServer() }) ``` -------------------------------- ### Usage with Custom Logger Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Configure closeWithGrace to use a custom logger for error messages. The default logger is console. ```javascript const closeWithGrace = require('close-with-grace') // delay is the number of milliseconds for the graceful close to // finish. closeWithGrace( { delay: 500, logger: { error: (m) => console.error(`[close-with-grace] ${m}`) } }, async function ({ signal, err, manual }) { if (err) { console.error(err) } await closeYourServer() }) ``` -------------------------------- ### Skipping Specific Events Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Prevent specific events like 'unhandledRejection' or 'uncaughtException' from triggering the graceful close. You must handle these skipped events manually. ```javascript import closeWithGrace from 'close-with-grace' // Handle errors separately process.on('unhandledRejection', (err) => { // Your custom error handling }) closeWithGrace( { skip: ['unhandledRejection', 'uncaughtException'] }, async function ({ signal, err, manual }) { await cleanupResources() } ) ``` -------------------------------- ### Disable Logger Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Disable logging entirely by passing a falsy value to the logger option. ```javascript // default logger is console // to disable logging at all, pass falsy value to logger option. closeWithGrace({ logger: false }, () => await somethingUseful()) ``` -------------------------------- ### Disable Delay Source: https://github.com/mcollina/close-with-grace/blob/main/README.md Disable the delay feature by passing a falsy value to the delay option. ```javascript // default delay is 10000 // to disable delay feature at all, pass falsy value to delay option. closeWithGrace({ delay: false }, () => await somethingUseful()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.