🔪🐛 Fix shutdown being called before init, fix duplicate SIGNIT catch
All checks were successful
Deploy application / deploy (push) Successful in 16s

This commit is contained in:
Martin Berg Alstad 2025-02-13 21:14:51 +01:00
parent 4367c24fb0
commit 95ddcbaf13
Signed by: martials
GPG Key ID: A3824877B269F2E2

View File

@ -28,7 +28,6 @@ async function main(): Promise<void> {
await runCronJob(bank) await runCronJob(bank)
} }
// TODO log the days the transactions are fetched
export async function moveTransactions( export async function moveTransactions(
actual: Actual, actual: Actual,
bank: Bank, bank: Bank,
@ -81,31 +80,32 @@ async function runOnce(bank: Bank) {
} }
async function runCronJob(bank: Bank): Promise<void> { async function runCronJob(bank: Bank): Promise<void> {
let actual: Actual | undefined
let cronJob: CronJob | undefined
logger.info("Waiting for CronJob to start") logger.info("Waiting for CronJob to start")
try {
// TODO move try-catch inside closure? const cronJob = cronJobDaily(async () => {
cronJob = cronJobDaily(async () => { let actual: Actual | undefined
try {
actual = await ActualImpl.init() actual = await ActualImpl.init()
await moveTransactions(actual, bank) await moveTransactions(actual, bank)
}) } catch (exception) {
registerInterrupt(bank, cronJob) logger.error(exception, "Caught exception at CronJob, shutting down!")
} catch (exception) { await shutdown(bank, cronJob)
logger.error(exception, "Caught exception at CronJob, shutting down!") } finally {
await shutdown(bank, cronJob) await actual?.shutdown()
} finally { }
// TODO shuts down immediatly, move into closure })
await actual?.shutdown() registerInterrupt(bank, cronJob)
}
} }
let isShuttingDown = false
function registerInterrupt( function registerInterrupt(
bank: Bank, bank: Bank,
cronJob: CronJob | undefined = undefined, cronJob: CronJob | undefined = undefined,
): void { ): void {
process.on("SIGINT", async () => { process.on("SIGINT", async () => {
if (isShuttingDown) return
isShuttingDown = true
logger.info("Caught interrupt signal") logger.info("Caught interrupt signal")
await shutdown(bank, cronJob) await shutdown(bank, cronJob)
}) })