🔪🐛 Fix shutdown being called before init, fix duplicate SIGNIT catch
All checks were successful
Deploy application / deploy (push) Successful in 16s
All checks were successful
Deploy application / deploy (push) Successful in 16s
This commit is contained in:
parent
4367c24fb0
commit
95ddcbaf13
18
src/main.ts
18
src/main.ts
@ -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")
|
||||||
|
|
||||||
|
const cronJob = cronJobDaily(async () => {
|
||||||
|
let actual: Actual | undefined
|
||||||
try {
|
try {
|
||||||
// TODO move try-catch inside closure?
|
|
||||||
cronJob = cronJobDaily(async () => {
|
|
||||||
actual = await ActualImpl.init()
|
actual = await ActualImpl.init()
|
||||||
await moveTransactions(actual, bank)
|
await moveTransactions(actual, bank)
|
||||||
})
|
|
||||||
registerInterrupt(bank, cronJob)
|
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
logger.error(exception, "Caught exception at CronJob, shutting down!")
|
logger.error(exception, "Caught exception at CronJob, shutting down!")
|
||||||
await shutdown(bank, cronJob)
|
await shutdown(bank, cronJob)
|
||||||
} finally {
|
} finally {
|
||||||
// TODO shuts down immediatly, move into closure
|
|
||||||
await actual?.shutdown()
|
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)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user