mcollina/close-with-grace is a JavaScript project with 304 stars. Exit your process, gracefully (if possible) - for Node.js
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Exit your process, gracefully (if possible) - for Node.js
Install
npm i close-with-grace
Usage
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()
})
// default delay is 10000
// to disable delay feature at all, pass falsy value to delay option.
closeWithGrace({ delay: false }, () => await somethingUseful())
Injecting custom logger
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()
})
// default logger is console
// to disable logging at all, pass falsy value to logger option.
closeWithGrace({ logger: false }, () => await somethingUseful())
Example with Fastify
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()
Skipping specific events
Skip specific events from triggering the graceful close. Note: You are responsible for handling skipped events yourself.
Note: You must handle skipped events yourself, otherwise they may cause the process to crash or exit unexpectedly.
onSecondError(error): A callback to execute if the process throws an uncaughtException
or an unhandledRejection while fn is executing.
onSecondSignal(signal): A callback to execute if the process receives another
signal while fn is executing.
onTimeout(delay): A callback to execute if fn failed to completed after delay milliseconds.
Both onSecondError, onSecondSignal or onTimeout can be used to perform custom logic, but process.exit(1)
will be immediately be invoked after they exit so no asynchronous operations are possible.
fn({ err, signal, manual } [, cb])
Execute the given function to perform a graceful close.
The function can either return a Promise or call the callback.
If this function does not error, the process will be closed with
exit code 0.
If the function rejects with an Error, or call the callback with an
Error as first argument, the process will be closed with exit code
1.
return values
Calling closeWithGrace() will return an object as formed:
close(): close the process, the manual argument will be set to
true.
Does mcollina/close-with-grace have a project website?
No homepage URL was recorded for mcollina/close-with-grace in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Is mcollina/close-with-grace open source?
Yes — mcollina/close-with-grace ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/mcollina/close-with-grace.
What is mcollina/close-with-grace?
mcollina/close-with-grace (mcollina/close-with-grace) is a JavaScript project on GitHub. From the project's own README: Exit your process, gracefully (if possible) - for Node.js
What license does mcollina/close-with-grace use?
mcollina/close-with-grace is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about mcollina/close-with-grace?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/mcollina/close-with-grace is the definitive source.
Read full README in the tab above.
Is close-with-grace worth your time?
ChatGPT, Claude and Perplexity can all read this page. Ask one of them what it makes of close-with-grace.