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

@digicat/termination-manager

v1.0.1

Published

The one true way of terminating services

Downloads

148

Readme

@digicat/termination-manager

The one true way of terminating services.

Termination tasks can be added with different priorities. Tasks are executed in descending priority order. Tasks with the same priority are terminated concurrently.

Takes callback functions which application code can use to log messages.

Supports configurable grace periods to ensure the process does terminate.

Handles SIGINT and SIGTERM signals and listens for uncaught exceptions and unhandled rejections. When these signals are received, the process is terminated gracefully. Uncaught exceptions and unhandled rejections cause the process to be terminated with a 0 millisecond grace period.

Requires Node.js 8.6 or later.

Usage

const TerminationManager = require('@digicat/termination-manager')

const manager = new TerminationManager({
  // Default grace period, in milliseconds
  grace: 5000,

  // How long to wait, in milliseconds, for the event loop to drain, before
  // forcibly terminating the process.
  drainGrace: 5000,

  // Called when it takes longer than `drainGrace` for the event loop to drain.
  // The process is terminated immediately after this callback returns.
  onDrainTimeout () {},

  // Called when the `SIGINT` signal is received. The process starts to
  // gracefully exit after this callback returns.
  onSigint () {},

  // Called when the `SIGTERM` signal is received. The process starts to
  // gracefully exit after this callback returns.
  onSigterm () {},

  // Called when an error occurs while stopping a task.
  onStopError (err) {},

  // Called when an uncaught exception occurs. The process is terminated with a
  // 0ms grace period and an exit code of 1 as soon as this callback returns.
  onUncaughtException (err) {},

  // Called when an unhandled rejection occurs. The process is terminated with a
  // 0ms grace period and an exit code of 1 as soon as this callback returns.
  onUnhandledRejection () {}
})

// Add a termination task, for instance to close a database connection. Returns
// the manager instance so calls can be chained.
manager.add({
  // The name must be a non-empty string.
  name: 'db'

  // Tasks are run in priority order. Priorities must be non-negative integers.
  // Defaults to 0.
  priority: 0,

  // The stop() method must implement the logic for gracefully stopping a
  // resource (e.g. a database connection). Receives the allowed grace period
  // (in milliseconds). If the method returns a promise, it must fulfil before
  // either 100ms or 110% of the grace period have passed (whichever is larger).
  //
  // Failure to do so causes a stop error. These can be identified by their
  // `name` property, which will be `StopTimeoutError`. The task object and
  // grace period are available through the `task` and `grace` properties
  // respectively.
  //
  // Exceptions are caught and also cause a stop error.
  //
  // Stop errors prevent any other tasks from running, leading to a disgraceful
  // termination. The exit code will be set to 1, unless it's already set to a
  // non-zero value.
  async stop (grace) {
    // …
  }
})

// Make the process terminate with an exit code of 1, and a grace period of 0ms.
// Termination tasks are still called but are given very little time to
// complete.
manager.crash()

// Make the process terminate with the given exit code and grace period. The
// grace period defaults to the one provided in the constructor. Can be called
// without any arguments.
manager.exit({code = 0, grace = 5000})