Skip to content

Commit

Permalink
feat: expose graceful shutdown method (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr authored Feb 21, 2024
1 parent bf2a903 commit 4730205
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/shutdown-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const shutdownConfigs: ShutdownConfig[] = [];

let isShuttingDown = false;

async function startShutdown() {
/**
* Start a graceful API shutdown.
*/
export async function shutdown(): Promise<void> {
if (isShuttingDown) {
return;
}
Expand Down Expand Up @@ -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);
}

0 comments on commit 4730205

Please sign in to comment.