Giter Site home page Giter Site logo

graceful-goodbye's Introduction

graceful-goodbye

Run cleanup logic just before the process exits without interfering with other process handlers.

npm install graceful-goodbye

Usage

import goodbye from 'graceful-goodbye'

goodbye(async function () {
  console.log('i am run before exit')
})

API

const unregister = goodbye(beforeExit, [position = 0])

Register an async function to be run before process exit.

  • Ran when SIGTERM/SIGINT is received
  • Ran if the event loop is about to end

If a process signal is received and graceful-goodbye is the only signal handler it mimicks the default behaivour of exiting the process when the exit handlers has run with a 130 exit code. If other handlers are registered, it defers to them to exit the process.

All handlers are deregistered when the beforeExit method runs, which means if the user sends two SIGINTs the second one will always exit the process immediately, assuming no other handlers are registered.

Note that the function is NOT run if the user calls process.exit or if an unhandled error occurs - this is by design. Those events should exit the process in the same tick as their occur.

goodbye.exit()

Triggers the cleanup logic (similar effect to receiving a process signal).

goodbye.exiting

Boolean if the exit code is running.

Position

goodbye(async () => console.log('last'), 2)
goodbye(async () => console.log('first'), 0)
goodbye(async () => console.log('middle'), 1)

The position value allows you to group handlers, they're executed and awaited by ascending order.

License

MIT

graceful-goodbye's People

Contributors

mafintosh avatar lukks avatar kasperisager avatar

Stargazers

Luka Radisavljevic avatar

Forkers

madjarx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.