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

@neezer/exit

v2.3.0

Published

> A helper to exit your application cleanly when SIGNAL is received.

Downloads

2

Readme

@neezer/exit

A helper to exit your application cleanly when SIGNAL is received.

Build Status semantic-release

Example:

Break down the component pieces of your application that need to gracefully exit into event emitters. Pass these event emitters to exit to wait for all of them to report a successful exit before shutting down the main application.

So, let's say you have a web app that is connected to a database and RabbitMQ. There are three component pieces to this application that need to be gracefully shutdown:

  1. The HTTP server
  2. The database connection pool
  3. The RabbitMQ client

In some cases, the instance of your client or server is already an event emitter. If so, you can define your handlers directly on the same object:

// file: httpServer.js

import { INITIATE_EXIT, COMPLETE_EXIT } from "@neezer/exit";
import { createServer } from "http";

const server = createServer(/* ... */);

server.on(INITIATE_EXIT, () => {
  server.close();
});

server.on("close", () => {
  server.emit(COMPLETE_EXIT);
});

export default server;

In some cases, you need to wrap the client/server interface in an EventEmitter yourself:

// file: db.js

import { INITIATE_EXIT, COMPLETE_EXIT } from "@neezer/exit";
import pgClient from "./pgClient";
import { EventEmitter } from "events";

const client = pgClient(/* ... */);
const status = new EventEmitter();

status.on(INITIATE_EXIT, () => {
  client.close().then(() => {
    status.emit(COMPLETE_EXIT);
  });
});

export default status;

Then—in your top-level entry file—you can pass these event emitters to exit:

// file: app.js

import { exit } from "@neezer/exit";
import http from "./httpServer";
import db from "./db";
import amqp from "./amqp";

exit(http, db, amqp);

Exit will configure listeners on the main Node thread for SIGINT & SIGKILL, then will emit INITIATE_EXIT on each of the provided event emitters above, and will wait for each of them to emit COMPLETE_EXIT after a certain period of time. When it collects all the completed events, it will exit the main process cleanly.

If one of the child pieces does not exit cleanly or times out, the library will exit the main application thread with a status code of 1.

Why?

I found myself copy-pasting a lot of the signal handling code from repo to repo, so extraced it out into this interface instead.