🔨 Run once, graceful shutdown, db filename in env
- Refactored toISODateString function - Added Db filename to env - Added getOrDefault function for fetching envs, replaced LOG_LEVEL with getOrDefault - Added script to run job once then exit - Catch SIGINT signal and do a graceful shutdown
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
import { type Dayjs } from "dayjs"
|
||||
|
||||
export function toISODateString(day: Dayjs): string {
|
||||
return `${day.year()}-${(day.month() + 1).toString().padStart(2, "0")}-${day.date().toString().padStart(2, "0")}`
|
||||
}
|
||||
export const toISODateString = (day: Dayjs): string => day.format("YYYY-MM-DD")
|
||||
|
@ -1,8 +1,9 @@
|
||||
import pino from "pino"
|
||||
import { LOG_LEVEL } from "../config.ts"
|
||||
|
||||
/**
|
||||
* / Returns a logging instance with the default log-level "info"
|
||||
*/
|
||||
export default pino({
|
||||
level: process.env.LOG_LEVEL as string || "info",
|
||||
level: LOG_LEVEL,
|
||||
})
|
||||
|
37
src/main.ts
37
src/main.ts
@ -10,6 +10,7 @@ import {
|
||||
ACTUAL_ACCOUNT_IDS,
|
||||
ACTUAL_DATA_DIR,
|
||||
BANK_ACCOUNT_IDS,
|
||||
DB_FILENAME,
|
||||
} from "../config.ts"
|
||||
import logger from "@/logger.ts"
|
||||
import type { UUID } from "node:crypto"
|
||||
@ -17,7 +18,6 @@ import { createDb } from "@/bank/db/queries.ts"
|
||||
import * as fs from "node:fs"
|
||||
|
||||
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
|
||||
// TODO create .cache if missing
|
||||
|
||||
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
||||
// Fetch transactions from the bank
|
||||
@ -59,29 +59,40 @@ function createCacheDirIfMissing(): void {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO add a script to run an immediate job, without cron
|
||||
// TODO catch ^C to stop server
|
||||
async function main(): Promise<void> {
|
||||
logger.info("Starting application")
|
||||
|
||||
createCacheDirIfMissing()
|
||||
|
||||
const actual = await ActualImpl.init()
|
||||
const databaseFileName = "default.sqlite" // TODO move name to env
|
||||
const databaseFileName = `${DB_FILENAME}.sqlite`
|
||||
const db = createDb(databaseFileName)
|
||||
logger.info(`Started SQLlite database with filename="${databaseFileName}"`)
|
||||
|
||||
process.on("SIGINT", async () => {
|
||||
logger.info("Caught interrupt signal")
|
||||
await shutdown()
|
||||
})
|
||||
|
||||
if (process.env.ONCE) {
|
||||
await daily(actual, new Sparebank1Impl(db))
|
||||
await shutdown()
|
||||
return
|
||||
}
|
||||
|
||||
logger.info("Waiting for CRON job to start")
|
||||
const cronJob = cronJobDaily(async () => {
|
||||
logger.info("Running daily job")
|
||||
await daily(actual, new Sparebank1Impl(db))
|
||||
logger.info("Finished daily job")
|
||||
})
|
||||
|
||||
// cronJobDaily(async () => {
|
||||
logger.info("Running daily job")
|
||||
await daily(actual, new Sparebank1Impl(db))
|
||||
logger.info("Finished daily job")
|
||||
// })
|
||||
|
||||
logger.info("Shutting down")
|
||||
await actual.shutdown()
|
||||
db.close()
|
||||
async function shutdown(): Promise<void> {
|
||||
logger.info("Shutting down")
|
||||
await actual.shutdown()
|
||||
db.close()
|
||||
cronJob.stop()
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
|
Reference in New Issue
Block a user