Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tizmagik/simply-graceful
:hibiscus: Asynchronous readiness/liveness probe management + graceful shutdown for NodeJS services
https://github.com/tizmagik/simply-graceful
Last synced: about 1 month ago
JSON representation
:hibiscus: Asynchronous readiness/liveness probe management + graceful shutdown for NodeJS services
- Host: GitHub
- URL: https://github.com/tizmagik/simply-graceful
- Owner: tizmagik
- Created: 2022-06-30T18:46:50.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-01T03:04:37.000Z (over 2 years ago)
- Last Synced: 2024-04-24T15:23:53.504Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 405 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# simply-graceful :hibiscus: [![npm version](https://badge.fury.io/js/simply-graceful.svg)](https://badge.fury.io/js/simply-graceful)
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
```bash
npm i simply-graceful
```or
```bash
yarn add simply-graceful
```## Usage
```ts
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 {
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](./src/index.ts#L13) type. More docs TK.