Graceful shutdown on exceptions
This commit is contained in:
parent
41cb92cce5
commit
9b0391be7c
12
src/main.ts
12
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 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 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 verbatimSyntax in tsconfig, conflicts with jest
|
||||||
// TODO multi module project. Main | DAL | Sparebank1 impl
|
// 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
|
// 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
|
let cronJob: CronJob | undefined
|
||||||
if (process.env.ONCE) {
|
if (process.env.ONCE) {
|
||||||
await daily(actual, bank)
|
try {
|
||||||
|
return await daily(actual, bank)
|
||||||
|
} finally {
|
||||||
await shutdown()
|
await shutdown()
|
||||||
return
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Waiting for CRON job to start")
|
logger.info("Waiting for CRON job to start")
|
||||||
// TODO init and shutdown resources when job runs?
|
// TODO init and shutdown resources when job runs?
|
||||||
|
try {
|
||||||
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
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> {
|
async function shutdown(): Promise<void> {
|
||||||
logger.info("Shutting down, Bye!")
|
logger.info("Shutting down, Bye!")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user