Skip to content

Commit

Permalink
feat: expose graceful shutdown method
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Feb 21, 2024
1 parent bf2a903 commit 58a8a7f
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> {

Check warning on line 21 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L18-L21

Added lines #L18 - L21 were not covered by tests
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();

Check warning on line 73 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L73

Added line #L73 was not covered by tests
});
});
process.once('unhandledRejection', error => {
logger.error(error, 'unhandledRejection');
logger.error('Shutting down... received unhandledRejection.');
void startShutdown();
void shutdown();

Check warning on line 79 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L79

Added line #L79 was not covered by tests
});
process.once('uncaughtException', error => {
logger.error(error, 'uncaughtException');
logger.error('Shutting down... received uncaughtException.');
void startShutdown();
void shutdown();

Check warning on line 84 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L84

Added line #L84 was not covered by tests
});
process.once('beforeExit', () => {
logger.error('Shutting down... received beforeExit.');
void startShutdown();
void shutdown();

Check warning on line 88 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L88

Added line #L88 was not covered by tests
});
}

export function registerShutdownConfig(...configs: ShutdownConfig[]) {
/**
* Register shutdown handlers for different API components.
* @param configs - Array of shutdown configurations
*/
export function registerShutdownConfig(...configs: ShutdownConfig[]): void {

Check warning on line 96 in src/shutdown-handler/index.ts

View check run for this annotation

Codecov / codecov/patch

src/shutdown-handler/index.ts#L92-L96

Added lines #L92 - L96 were not covered by tests
registerShutdownSignals();
shutdownConfigs.push(...configs);
}

0 comments on commit 58a8a7f

Please sign in to comment.