Graceful shutdown on exceptions

This commit is contained in:
Martin Berg Alstad 2025-02-02 11:22:29 +01:00
parent 41cb92cce5
commit 9b0391be7c
Signed by: martials
GPG Key ID: A3824877B269F2E2

View File

@ -21,7 +21,6 @@ import { CronJob } from "cron"
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
// TODO move tsx to devDependency. Requires ts support for Node with support for @ alias
// TODO global exception handler, log and graceful shutdown
// TODO verbatimSyntax in tsconfig, conflicts with jest
// TODO multi module project. Main | DAL | Sparebank1 impl
// TODO store last fetched date in db, and refetch from that date, if app has been offline for some time
@ -82,14 +81,21 @@ async function main(): Promise<void> {
let cronJob: CronJob | undefined
if (process.env.ONCE) {
await daily(actual, bank)
await shutdown()
return
try {
return await daily(actual, bank)
} finally {
await shutdown()
}
}
logger.info("Waiting for CRON job to start")
// TODO init and shutdown resources when job runs?
cronJob = cronJobDaily(async () => await daily(actual, bank))
try {
cronJob = cronJobDaily(async () => await daily(actual, bank))
} catch (exception) {
logger.error(exception, "Caught exception at daily job, shutting down!")
await shutdown()
}
async function shutdown(): Promise<void> {
logger.info("Shutting down, Bye!")