From 6ff9f4e18d7fea9febee43df52e95d44a927ef75 Mon Sep 17 00:00:00 2001 From: adityapk00 <31996805+adityapk00@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:59:09 -0700 Subject: [PATCH] fix: Add a grace period before forcing exit after signals (#682) --- apps/hubble/src/cli.ts | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/apps/hubble/src/cli.ts b/apps/hubble/src/cli.ts index 6f0f1e4a..40cc69f5 100644 --- a/apps/hubble/src/cli.ts +++ b/apps/hubble/src/cli.ts @@ -22,6 +22,10 @@ const DEFAULT_PEER_ID_FILENAME = `default_${PEER_ID_FILENAME}`; const DEFAULT_PEER_ID_LOCATION = `${DEFAULT_PEER_ID_DIR}/${DEFAULT_PEER_ID_FILENAME}`; const DEFAULT_CHUNK_SIZE = 10000; +// Grace period before exiting the process after receiving a SIGINT or SIGTERM +const SHUTDOWN_GRACE_PERIOD_MS = 30_000; +let isExiting = false; + const app = new Command(); app.name('hub').description('Farcaster Hub').version(APP_VERSION); @@ -62,6 +66,27 @@ app process.exit(); }; + const handleShutdownSignal = (signal: NodeJS.Signals) => { + logger.warn(`${signal} received`); + if (!isExiting) { + isExiting = true; + teardown(hub) + .then(() => { + logger.info('Hub stopped gracefully'); + process.exit(0); + }) + .catch((err) => { + logger.error({ reason: `Error stopping hub: ${err}` }); + process.exit(1); + }); + + setTimeout(() => { + logger.fatal('Forcing exit after grace period'); + process.exit(1); + }, SHUTDOWN_GRACE_PERIOD_MS); + } + }; + // try to load the config file const hubConfig = (await import(resolve(cliOptions.config))).Config; @@ -150,19 +175,16 @@ app process.stdin.resume(); - process.on('SIGINT', async () => { - logger.fatal('SIGINT received'); - await teardown(hub); + process.on('SIGINT', () => { + handleShutdownSignal('SIGINT'); }); - process.on('SIGTERM', async () => { - logger.fatal('SIGTERM received'); - await teardown(hub); + process.on('SIGTERM', () => { + handleShutdownSignal('SIGTERM'); }); - process.on('SIGQUIT', async () => { - logger.fatal('SIGQUIT received'); - await teardown(hub); + process.on('SIGQUIT', () => { + handleShutdownSignal('SIGQUIT'); }); process.on('uncaughtException', (err) => {