Compare commits
2 Commits
db30129c30
...
06cc89f762
Author | SHA1 | Date | |
---|---|---|---|
06cc89f762 | |||
b6daf4268c |
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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>,
|
||||
|
26
src/main.ts
26
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<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()
|
||||
}
|
||||
|
@ -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()
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user