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

@unboundedsystems/node-graceful

v3.0.0-unb.2

Published

Graceful process exit manager. allows waiting on multiple async services.

Downloads

582

Readme

node-graceful

Build Status npm npm bundle size (version)

node-graceful is a small helper module without dependencies that aims to ease graceful exit of complex node programs including async waiting on multiple independent modules.

Installation:

npm i -S node-graceful

yarn add node-graceful

Had any problem? open an issue

Quick example

const Graceful = require('node-graceful');
Graceful.captureExceptions = true;

Graceful.on('exit', async () => {
    console.log(`Received ${signal} - Exiting gracefully`);
    await webServer.close(); 
});

//  Graceful will wait until all listeners had finished
Graceful.on('exit', (signal) => {
    return new Promise((resolve) => {
        console.log("Another independent listener!");
        setTimeout(() => resolve(), 1000);    
    });
});

Typescript

import Graceful from 'node-graceful';
Graceful.captureExceptions = true;

Graceful.on('exit', async () => {
  await server.close();
});

Quick Docs

interface Graceful {
    // add exit listener
    on(signal: 'exit', listener: GracefulListener): GracefulSubscription;
    // remove exit listener
    off(signal: 'exit', listener: GracefulListener): void;
    // remove all exit listeners
    clear(): void;
    // trigger graceful process exit with or without exit code and signal
    exit(): void;
    exit(exitCode: number): void;
    exit(exitSignal: string): void;
    exit(exitCode: number, exitSignal: string): void;

    // whether to exit immediately when a second kill signal is received
    exitOnDouble: boolean; // default: true
    // maximum time to wait before hard-killing the process
    timeout: number; // default: 30000
    // whether to treat uncaught exceptions as process terminating events
    captureExceptions: boolean; // default: false
    // whether to treat unhandled promise rejections as process terminating events
    captureRejections: boolean; // default: false
}

type GracefulListener = (signal: string, details?: object) => (void | any | Promise<any> | Promise<Error>);
type GracefulSubscription = () => void;

Read bellow for full API reference.

API Reference

Graceful.on('exit', {Function} listener)

Add exit listener to be called when process exit is triggered. Graceful listens on all terminating signals and triggers exit accordingly.

Terminating events: SIGTERM SIGINT SIGBREAK SIGHUP

Options

  • listener(signal, details?) - listener function
    • signal - the signal that triggered the exit. example: 'SIGTERM'
    • details - optional details provided by the trigger. for example in case of unhandledException this will be an error object. on external signal it will be undefined.

Examples

The listener function can return a promise that will delay the process exit until it's fulfilment.

Graceful.on('exit', () => Promise.resolve('I Am A Promise!'));
Graceful.on('exit', async () => {
  // async function always returns promise so shutdown will be delayed until this functions ends
  await webServer.close();

  return Promise.all([
    controller.close(),
    dbClient.close()
  ]);
});

if old style callback is needed, wrap the logic with a promise

const server = require('http').createServer(function (req, res) {
    res.write('ok');
    res.end()
})
Graceful.on('exit', () => {
  return new Promise((resolve, reject) => {
    server.close((err) => {
      if (err) return reject(err);
      resolve();
    });
  });
});

Return value

the method returns a function that when invoked, removes the listener subscription. the function is a shorthand for .off method

example
// use the return value to remove listener
const removeListener = Graceful.on('exit', () => {});
removeListener(); // listener was removed and will not be triggered

Graceful.off('exit', {Function} listener)

Remove a previously subscribed listener.

example
const gracefulExit = () => {
    console.log("exiting!");
};

// add listener
let removeListener = Graceful.on('SIGTERM', gracefulExit);

// remove listener
Graceful.off('SIGTERM', gracefulExit);
// same as invoking the return value
// removeListener();

Graceful.clear()

Unsubscribe all exit listeners.

example
// add listener
Graceful.on('exit', () => {
   console.log("Received some exit signal!");
   return Promise.resolve("A promise to be waited on before dying");
});

Graceful.on('exit', (done) => {
   console.log("Another listener");
   done();
});

// remove all listener
Graceful.clear();

Graceful.exit({Number} [code], {String} [signal])

Trigger graceful process exit. This method is meant to be a substitute command for process.exit() to allow other modules to exit gracefully in case of error.

  • code - (optional) exit code to be used. default - process.exitCode
  • signal - (optional) signal to be simulating for listeners. default - SIGTERM
example

server.listen(3333)
        .on('listening', function () {
            console.log('Yay!')
        })
        .on('error', function (err) {
            if (err.code === 'EADDRINUSE') {
                console.error("Damn, Port is already in use...");
                Graceful.exit();
            }
        });

// exit code and signal can be specified
// Graceful.exit(1);
// Graceful.exit(1, 'SIGINT');
// Graceful.exit('SIGINT');

Options

Options are global and shared, any change will override previous values.

Graceful.exitOnDouble = true {boolean}

Whether to exit immediately when a second deadly event is received, For example when Ctrl-C is pressed twice etc.. When exiting due to double event, exit code will be process.exitCode or 1 (necessarily a non-zero)

Graceful.timeout = 30000 {number}

Maximum time to wait for exit listeners in ms. After exceeding the time, the process will force exit and the exit code will be process.exitCode or 1 (necessarily a non-zero)

Setting the timeout to 0 will disable timeout functionality (will wait indefinitely)

Graceful.captureExceptions = false {boolean}

Whether to treat uncaughtException event as a terminating event and trigger graceful shutdown.

Graceful.captureExceptions = true;

throw new Error('DANG!'); // this will now trigger graceful shutdown 

Graceful.captureExceptions = false {boolean}

Whether to treat unhandledRejection event as a terminating event and trigger graceful shutdown. On newer node versions unhandledRejection is in-fact a terminating event

Graceful.captureRejections = true;

Promise.reject(new Error('DANG!')); // this will now trigger graceful shutdown 

exitCode

Graceful will obey process.exitCode property value when exiting unless the exit is forced (double signal, timeout) in which case the exit code must be non-zero.