npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

close-with-grace

v2.1.0

Published

Exit your process, gracefully (if possible)

Downloads

731,028

Readme

close-with-grace

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()

API

closeWithGrace([opts], fn({ err, signal, manual }))

closeWithGrace adds a global listeners to the events:

  • process.once('SIGHUP')
  • process.once('SIGINT')
  • process.once('SIGQUIT')
  • process.once('SIGILL')
  • process.once('SIGTRAP')
  • process.once('SIGABRT')
  • process.once('SIGBUS')
  • process.once('SIGFPE')
  • process.once('SIGSEGV')
  • process.once('SIGUSR2')
  • process.once('SIGTERM')
  • process.once('uncaughtException')
  • process.once('unhandledRejection')
  • process.once('beforeExit')

In case one of them is emitted, it will call the given function. If it is emitted again, it will terminate the process abruptly.

opts

  • delay: the numbers of milliseconds before abruptly close the process. Default: 10000.

    • Pass false, null or undefined to disable this feature.
  • logger: instance of logger which will be used internally. Default: console.

    • Pass false, null or undefined to disable this feature.

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.
  • uninstall(): remove all global listeners.

License

MIT