From b6daf4268ccb14a46192d8e8fdea079dfba86457 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Sun, 9 Feb 2025 13:34:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Init=20and=20shutdown=20Actual?= =?UTF-8?q?=20on=20job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Renamed daily function to moveTransactions - testConnections method to verify connection immediately --- src/actual.ts | 15 +++++++++++++++ src/main.ts | 24 +++++++++++++++++------- tests/main.test.ts | 4 ++-- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/actual.ts b/src/actual.ts index 8bb8a13..b623db8 100644 --- a/src/actual.ts +++ b/src/actual.ts @@ -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 { + 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, diff --git a/src/main.ts b/src/main.ts index a9a3a67..8bc4a67 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { +export async function moveTransactions( + actual: Actual, + bank: Bank, +): Promise { // Fetch transactions from the bank const actualTransactions = await bank.fetchTransactions( relativeInterval(), @@ -63,7 +66,6 @@ async function main(): Promise { 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 { 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 { logger.info("Shutting down, Bye!") - await actual.shutdown() db.close() cronJob?.stop() } diff --git a/tests/main.test.ts b/tests/main.test.ts index a1b6d09..3246738 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -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() }) })