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

@routes/graceful-shutdown

v2.1.0

Published

πŸ’€ Shut down server gracefully

Downloads

10

Readme

@routes/graceful-shutdown

πŸ’€ Shut down server gracefully

net.Server or Express, whatever you're using should be fine

const graceful = require('@routes/graceful-shutdown');

const server = app.listen(1337); // express will return the server instance here
graceful(server);

Arguments

  • First argument is an net.Server instance (including Express server)
  • Second argument is options:

| option | type | meaning | default | - | - | - | - | timeout | Number | Time (in milliseconds) to wait before forcefully shutting down | 10000 (10 sec) | logger | Object | Object with info and error functions (supports async loggers). Pass false to disable | console | events | Array | Process events to handle | ['SIGTERM', 'SIGINT'] | onsuccess | Function | Final functionality when shutdown finished correctly | process.exit(0) | onfail | Function | Final functionality when shutdown finished incorrectly | process.exit(1)

Example of using options

graceful(server, {
	timeout: 3e4,
	logger: winston.createLogger({level: 'error'}),
});

What happens on process termination?

  1. Process termination is interrupted
  2. Open connections are instructed to end (FIN packet) and receive a new timeout to allow them to close in time
  3. The server is firing a close function
  4. After correct closing: onsuccess (default: process exists with exit code 0)
    • End correct behaviour
  5. After timeout passes - an error log is printed with the amount of connection that will be forcefully terminated
  6. onfail: (default: process exists with an exit code 1)

Add custom functionality to shutdown

Add behaviour to the graceful shut down process using a built in pub/sub mechanism

const { sub, BEFORE, AFTER } = graceful(server, {...});

// Will be triggered first thing before the procedure starts
sub(BEFORE, async() => {
	await flushThrottledThings();
	await closeDatabaseConnections();
});

// Will be triggered once the procedure has ended
sub(AFTER, () => logger.info('Okay okay, closing down'));

server.shuttingDown

After shutdown has initiated, the attribute shuttingDown is attached to server with value of true.

User can query this value on service to know not to send any more requests to the service

app.get(
	'/health',
	(request, response) => response.status(server.shuttingDown ? 503 : 200).end()
);

What else does graceful expose?

  • {Set} sockets A reference to the sockets collection
const { sockets } = graceful(server, {...});

// Monitor size of set every two minutes
setInterval(() => stats.time('graceful_stored_sockets', sockets.size), 12e4);