From 95ddcbaf13241e5852fa1cff4df66574941d8b95 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Thu, 13 Feb 2025 21:14:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=AA=F0=9F=90=9B=20Fix=20shutdown=20bei?= =?UTF-8?q?ng=20called=20before=20init,=20fix=20duplicate=20SIGNIT=20catch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4b83c7b..e1848f2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,7 +28,6 @@ async function main(): Promise { await runCronJob(bank) } -// TODO log the days the transactions are fetched export async function moveTransactions( actual: Actual, bank: Bank, @@ -81,31 +80,32 @@ async function runOnce(bank: Bank) { } async function runCronJob(bank: Bank): Promise { - let actual: Actual | undefined - let cronJob: CronJob | undefined - logger.info("Waiting for CronJob to start") - try { - // TODO move try-catch inside closure? - cronJob = cronJobDaily(async () => { + + const cronJob = cronJobDaily(async () => { + let actual: Actual | undefined + try { actual = await ActualImpl.init() await moveTransactions(actual, bank) - }) - registerInterrupt(bank, cronJob) - } catch (exception) { - logger.error(exception, "Caught exception at CronJob, shutting down!") - await shutdown(bank, cronJob) - } finally { - // TODO shuts down immediatly, move into closure - await actual?.shutdown() - } + } catch (exception) { + logger.error(exception, "Caught exception at CronJob, shutting down!") + await shutdown(bank, cronJob) + } finally { + await actual?.shutdown() + } + }) + registerInterrupt(bank, cronJob) } +let isShuttingDown = false + function registerInterrupt( bank: Bank, cronJob: CronJob | undefined = undefined, ): void { process.on("SIGINT", async () => { + if (isShuttingDown) return + isShuttingDown = true logger.info("Caught interrupt signal") await shutdown(bank, cronJob) })