simply-graceful
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

simply-graceful 🌺 npm version

A simple utility that provides asynchronous liveness and readiness probe management. It also handles graceful server close/process exiting to allow current requests in flight to complete in a best effort.

Installation

npm i simply-graceful

or

yarn add simply-graceful

Usage

import express from "express";
import type { Server } from "http";
import SimplyGraceful from "simply-graceful";
import { someMiddleware, anotherMiddleware } from "./middlewares";

export default async function CreateServer(): Promise<Server> {
  const { PORT = 3000, ENV = "development" } = process.env;

  const app = express();

  const graceful = new SimplyGraceful({
    app,
    skipProcessSignals: ENV !== "production",
    logger: console,
    livePath: "/.live",
    readyPath: "/.ready",
    grace: 5_000,
    delay: 30_000,
  });

  // someMiddleware here may do async stuff, so we have it return a callback
  // to signal ready when it's done
  graceful.waitForReady("someMiddleware"); // wait for someMiddleware to signal ready
  app.use(
    someMiddleware(() => {
      graceful.signalReady("someMiddleware"); // signal ready when someMiddleware is ready
    })
  );

  // similarly, anotherMiddleware may do async stuff, but we'll use a promise pattern
  // instead of a callback (just to illustrate)
  graceful.waitForReady("anotherMiddleware");
  app.use(
    anotherMiddleware().then(() => {
      graceful.signalReady("anotherMiddleware"); // signal ready when anotherMiddleware is ready
    })
  );

  const server = app.listen({ port: PORT }, (): void => {
    console.log(`🚀  Server ready at http://localhost:${PORT}/`);
  });

  // Pass `server` to graceful so it can shutdown when it receives a signal
  graceful.setServer(server);

  return server;
}

Config

Configuration options can be seen in SimplyGracefulConfig type. More docs TK.

Package Sidebar

Install

npm i simply-graceful

Weekly Downloads

0

Version

1.0.3

License

ISC

Unpacked Size

49.3 kB

Total Files

11

Last publish

Collaborators

  • tizmagik