From 58a8a7f72652359a9f567c660f6f39a1c802c249 Mon Sep 17 00:00:00 2001 From: Rafael Cardenas Date: Wed, 21 Feb 2024 14:20:59 -0600 Subject: [PATCH] feat: expose graceful shutdown method --- 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); }