From 4730205c22e2d747c4cc24ffdcbe5bf7889f223a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Wed, 21 Feb 2024 14:32:43 -0600 Subject: [PATCH] feat: expose graceful shutdown method (#19) --- src/shutdown-handler/index.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/shutdown-handler/index.ts b/src/shutdown-handler/index.ts index d7757a9..589e8c0 100644 --- a/src/shutdown-handler/index.ts +++ b/src/shutdown-handler/index.ts @@ -15,7 +15,10 @@ const shutdownConfigs: ShutdownConfig[] = []; let isShuttingDown = false; -async function startShutdown() { +/** + * Start a graceful API shutdown. + */ +export async function shutdown(): Promise { if (isShuttingDown) { return; } @@ -67,26 +70,30 @@ function registerShutdownSignals() { SHUTDOWN_SIGNALS.forEach(sig => { process.once(sig, () => { logger.info(`Shutting down... received signal: ${sig}`); - void startShutdown(); + void shutdown(); }); }); process.once('unhandledRejection', error => { logger.error(error, 'unhandledRejection'); logger.error('Shutting down... received unhandledRejection.'); - void startShutdown(); + void shutdown(); }); process.once('uncaughtException', error => { logger.error(error, 'uncaughtException'); logger.error('Shutting down... received uncaughtException.'); - void startShutdown(); + void shutdown(); }); process.once('beforeExit', () => { logger.error('Shutting down... received beforeExit.'); - void startShutdown(); + void shutdown(); }); } -export function registerShutdownConfig(...configs: ShutdownConfig[]) { +/** + * Register shutdown handlers for different API components. + * @param configs - Array of shutdown configurations + */ +export function registerShutdownConfig(...configs: ShutdownConfig[]): void { registerShutdownSignals(); shutdownConfigs.push(...configs); }