spearkit
Guides

Graceful shutdown

A Ctrl-C or a container stop sends your process a signal. If you don't handle it, the process dies mid-flight — the gateway connection, scheduler timers, and any open database…

On a SpearClient

client.enableGracefulShutdown({
  onShutdown: () => db.close(), // flush state before we exit
});
await client.start();

Progress is logged through client.logger. The method returns a disposer that removes the signal handlers (useful for tests / hot-reload).

Standalone

gracefulShutdown(client, options) works with any object that has a destroy() method:

import { gracefulShutdown } from "spearkit";

gracefulShutdown(client, { onShutdown: () => db.close() });

Options

FieldDefaultMeaning
signals["SIGINT", "SIGTERM"]Signals to listen for.
timeoutMs10000Force-exit if shutdown exceeds this.
onShutdownRuns before destroy(); receives the signal.
exittrueCall process.exit() when done (set false in tests).
logger{ info?, error? } progress logger.

Shutdown runs once — repeated signals during teardown are ignored.

On this page