diff --git a/src/main.ts b/src/main.ts index f26d3f3..be60b59 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { 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 { logger.info("Shutting down, Bye!")