Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose graceful shutdown method #19

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
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);
}
Loading