🚀 Init and shutdown Actual on job

- Renamed daily function to moveTransactions
- testConnections method to verify connection immediately
This commit is contained in:
Martin Berg Alstad 2025-02-09 13:34:00 +01:00
parent db30129c30
commit b6daf4268c
Signed by: martials
GPG Key ID: A3824877B269F2E2
3 changed files with 34 additions and 9 deletions

View File

@ -50,6 +50,21 @@ export class ActualImpl implements Actual {
return new ActualImpl()
}
/**
* Attempts to connect then immediatly shutsdown the connection
* @exception error If connection fails
*/
static async testConnection(): Promise<void> {
let actual: Actual | undefined
logger.info("Testing ActualBudget connection")
try {
actual = await ActualImpl.init()
} finally {
await actual?.shutdown()
logger.info("Finished testing ActualBudget connection")
}
}
async importTransactions(
accountId: UUID,
transactions: Iterable<ActualTransaction>,

View File

@ -22,7 +22,10 @@ import dayjs from "dayjs"
// TODO store last fetched date in db, and refetch from that date, if app has been offline for some time
// TODO do not fetch if saturday or sunday
export async function daily(actual: Actual, bank: Bank): Promise<void> {
export async function moveTransactions(
actual: Actual,
bank: Bank,
): Promise<void> {
// Fetch transactions from the bank
const actualTransactions = await bank.fetchTransactions(
relativeInterval(),
@ -63,7 +66,6 @@ async function main(): Promise<void> {
createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
const actual = await ActualImpl.init()
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
const db = createDb(databaseFilePath)
logger.info(`Started Sqlite database at '${databaseFilePath}'`)
@ -76,25 +78,33 @@ async function main(): Promise<void> {
let cronJob: CronJob | undefined
if (process.env.ONCE) {
const actual = await ActualImpl.init()
try {
return await daily(actual, bank)
return await moveTransactions(actual, bank)
} finally {
await actual.shutdown()
await shutdown()
}
} else {
await ActualImpl.testConnection()
}
logger.info("Waiting for CRON job to start")
// TODO init and shutdown resources when job runs?
let actual: Actual | undefined
try {
cronJob = cronJobDaily(async () => await daily(actual, bank))
cronJob = cronJobDaily(async () => {
actual = await ActualImpl.init()
await moveTransactions(actual, bank)
})
} catch (exception) {
logger.error(exception, "Caught exception at daily job, shutting down!")
logger.error(exception, "Caught exception at CronJob, shutting down!")
await shutdown()
} finally {
await actual?.shutdown()
}
async function shutdown(): Promise<void> {
logger.info("Shutting down, Bye!")
await actual.shutdown()
db.close()
cronJob?.stop()
}

View File

@ -1,6 +1,6 @@
import { describe, it } from "@jest/globals"
import { daily } from "@/main.ts"
import { moveTransactions } from "@/main.ts"
import { ActualImpl } from "@/actual.ts"
import { BankStub } from "./stubs/bankStub.ts"
@ -10,7 +10,7 @@ import { BankStub } from "./stubs/bankStub.ts"
describe("Main logic of the application", () => {
it("should import the transactions to Actual Budget", async () => {
const actual = await ActualImpl.init()
await daily(actual, new BankStub())
await moveTransactions(actual, new BankStub())
await actual.shutdown()
})
})