Skip to main content

@rtorcato/api-graceful-shutdown v1.0.0

Interfaces

ShutdownOptions

Defined in: index.ts:6

Properties

signals?

optional signals?: Signals[]

Defined in: index.ts:8

Signals that trigger shutdown. Default: ['SIGTERM', 'SIGINT'].

timeoutMs?

optional timeoutMs?: number

Defined in: index.ts:10

Hard limit for all hooks combined; exceeding it force-exits with code 1. Default: 10_000.

logger?

optional logger?: (message) => void

Defined in: index.ts:12

Called with human-readable progress lines. Default: no-op.

Parameters
message

string

Returns

void

exit?

optional exit?: (code) => void

Defined in: index.ts:14

Exit function — injectable for tests. Default: process.exit.

Parameters
code

number

Returns

void


ShutdownController

Defined in: index.ts:17

Properties

register

register: (name, hook) => void

Defined in: index.ts:19

Register a cleanup hook. Hooks run in registration order on shutdown.

Parameters
name

string

hook

ShutdownHook

Returns

void

shutdown

shutdown: (signal?) => Promise<void>

Defined in: index.ts:21

Trigger shutdown manually (signals do this automatically). Idempotent.

Parameters
signal?

string

Returns

Promise<void>

stop

stop: () => void

Defined in: index.ts:23

Remove the signal listeners this controller installed (mainly for tests).

Returns

void

Type Aliases

ShutdownHook

ShutdownHook = () => void | Promise<void>

Defined in: index.ts:4

A cleanup hook. Return (or resolve) when the resource is closed; throw or reject to mark it failed.

Returns

void | Promise<void>

Functions

createShutdownController()

createShutdownController(options?): ShutdownController

Defined in: index.ts:35

Install SIGTERM/SIGINT handlers that drain the process gracefully: run every registered cleanup hook in order, then exit. If the hooks take longer than timeoutMs, force-exit with code 1 so a stuck resource can't wedge a container restart.

Framework-agnostic — pass server.close, an AMQP connection.close, a DB pool end, etc. as hooks. Use closeHttpServer(server) for the common case.

Parameters

options?

ShutdownOptions = {}

Returns

ShutdownController


closeHttpServer()

closeHttpServer(server): ShutdownHook

Defined in: index.ts:111

A shutdown hook that closes a Node HTTP server — stops accepting new connections and resolves once in-flight requests finish. Works for Express (app.listen(...)) and Hono via @hono/node-server (serve(...)).

Parameters

server

Server

Returns

ShutdownHook