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

@codersyndicate/graceful-shutdown

v1.1.1

Published

Module that listens on posix signals and gracefully shutdowns the given server

Downloads

11

Readme

graceful-shutdown

Concept

A Graceful Shutdown allows the process to finish processing pending responses and release used ressources before been killed

In Kubernetes a Service Pod can be killed any time due to scaling or any other automated/manual administration command, the service process must be able to support the Kubernetes Pod Lifecycle by providing 2 probe routes: liveness and readiness

  • liveness only refects that the process exists, is stable and is accessible
  • readiness shows that the service dependencies (DB, ...) are available and is ready to process requests

While READY a pod will be in the service pod pool to which the traffic is directed and will receive requests to process, on the other hand while NOT-READY it will leave the pool, receive no new traffic and have a grace periode to finish processing pending responses and release used resources (DB connections, ...) before been shut down

Features

Multi Framework Support

The graceful shutdown logic is applied to the net.Server object, which means that all service frameworks should be supported.

Startup Readiness Checks

Function list that is used by the readiness route to assess if start conditions are met.

Shutdown Finalizers

Function list that has to be executed in parallel on shutdown. As an example, you may want to push your metrics before shutdown in order to avoid metric gaps.

Flowchart

graceful-shutdown-flowchart

Liveness

a route /health returning a Response Status 200 and Body OK as soon as the service port is open

Readiness

a route /health/readiness returning a Response Status 200 and Body READY as soon as all service dependencies are available (DB connections, ...), 503 and NOT-READY otherwise

Options

  • gracePeriodMilliseconds: grace period in milliseconds, must be longer than the average processing time (default: 5000)
  • finalizers: an array of functions, taking "server" and "callback" as arguments, to be executed on shutdown.

Knowledge

  • https://blog.laputa.io/graceful-shutdown-in-kubernetes-85f1c8d586da
  • https://blog.risingstack.com/graceful-shutdown-node-js-kubernetes/
  • https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace
  • https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html

Usage

const astalavista = require('@codersyndicate/graceful-shutdown');
const someDBLib = require('something'); // optional: service may have no dependencies

const options = {
    gracePeriodMilliseconds: 10000,
    finalizers: [
        function doSomethingUseful(server, callback) {
            console.log('i am dead...')
            callback();
        },
    ],
    readinessChecks: [
        function checkDBReadiness(server, callback) {
            if (ready) {
                callback();
                return;
            }
            
            callback(new Error('DB not reachable'));
        }
    ],
}

// astalavista returns a ServerGracefulShutdown instance
let graceful = astalavista.enable(server, options);

server.use('/health', graceful.liveliness);

// add a finalizer after initialisation
graceful.addFinalizer(function doSomethingEvenBetter(server, callback) {
  console.log('i shall return! (famous last words)')
  callback();
});

you have two possibilities to implement the readiness route

// use existing readiness check handler
server.use('/health/readiness', graceful.readiness);

// use a custom readiness check handler
server.use('/health/readiness', (req, res) => {
    if (astalavista.isTerminated()) {
        // service has been terminated by an external signal
        // this condition is mandatory
        return res.send(503, 'NOT-READY');
    }

    graceful.checkReadiness((error) => {
        if (error !== undefined) {
            response.send(503, 'NOT-READY');
            return;
        }

        request.send(200, 'READY');
    });
});

// add a readiness check after initialisation
graceful.addReadinessCheck(function checkSomethingElse(callback) {
    console.log('does it work?')
    callback();
});