Compare commits

...

2 Commits

Author SHA1 Message Date
06cc89f762
💾 Volume for cache, default values for envs in compose
All checks were successful
Deploy application / deploy (push) Successful in 3s
2025-02-09 13:54:35 +01:00
b6daf4268c
🚀 Init and shutdown Actual on job
- Renamed daily function to moveTransactions
- testConnections method to verify connection immediately
2025-02-09 13:34:00 +01:00
5 changed files with 40 additions and 12 deletions

View File

@ -25,6 +25,8 @@ jobs:
LOG_LEVEL: ${{ vars.LOG_LEVEL }}
DB_DIRECTORY: ${{ vars.DB_DIRECTORY }}
DB_FILENAME: ${{ vars.DB_FILENAME }}
TRANSACTION_RELATIVE_FROM_DATE: ${{ vars.TRANSACTION_RELATIVE_FROM_DATE }}
TRANSACTION_RELATIVE_TO_DATE: ${{ vars.TRANSACTION_RELATIVE_TO_DATE }}
steps:
- name: Check out repository code

View File

@ -21,8 +21,9 @@ services:
- TRANSACTION_RELATIVE_FROM_DATE
- TRANSACTION_RELATIVE_TO_DATE
volumes:
# TODO CACHE directory in volume?
- data:/${DB_DIRECTORY}
- cache:/${ACTUAL_DATA_DIR:-.cache}
- data:/${DB_DIRECTORY:-data}
volumes:
cache:
data:

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?
logger.info("Waiting for CronJob to start")
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()
})
})