🚀 Init and shutdown Actual on job
- Renamed daily function to moveTransactions - testConnections method to verify connection immediately
This commit is contained in:
parent
db30129c30
commit
b6daf4268c
@ -50,6 +50,21 @@ export class ActualImpl implements Actual {
|
|||||||
return new ActualImpl()
|
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(
|
async importTransactions(
|
||||||
accountId: UUID,
|
accountId: UUID,
|
||||||
transactions: Iterable<ActualTransaction>,
|
transactions: Iterable<ActualTransaction>,
|
||||||
|
24
src/main.ts
24
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 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
|
// 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
|
// Fetch transactions from the bank
|
||||||
const actualTransactions = await bank.fetchTransactions(
|
const actualTransactions = await bank.fetchTransactions(
|
||||||
relativeInterval(),
|
relativeInterval(),
|
||||||
@ -63,7 +66,6 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
|
createDirsIfMissing(ACTUAL_DATA_DIR, DB_DIRECTORY)
|
||||||
|
|
||||||
const actual = await ActualImpl.init()
|
|
||||||
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
|
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
|
||||||
const db = createDb(databaseFilePath)
|
const db = createDb(databaseFilePath)
|
||||||
logger.info(`Started Sqlite database at '${databaseFilePath}'`)
|
logger.info(`Started Sqlite database at '${databaseFilePath}'`)
|
||||||
@ -76,25 +78,33 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
let cronJob: CronJob | undefined
|
let cronJob: CronJob | undefined
|
||||||
if (process.env.ONCE) {
|
if (process.env.ONCE) {
|
||||||
|
const actual = await ActualImpl.init()
|
||||||
try {
|
try {
|
||||||
return await daily(actual, bank)
|
return await moveTransactions(actual, bank)
|
||||||
} finally {
|
} finally {
|
||||||
|
await actual.shutdown()
|
||||||
await shutdown()
|
await shutdown()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
await ActualImpl.testConnection()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Waiting for CRON job to start")
|
logger.info("Waiting for CRON job to start")
|
||||||
// TODO init and shutdown resources when job runs?
|
let actual: Actual | undefined
|
||||||
try {
|
try {
|
||||||
cronJob = cronJobDaily(async () => await daily(actual, bank))
|
cronJob = cronJobDaily(async () => {
|
||||||
|
actual = await ActualImpl.init()
|
||||||
|
await moveTransactions(actual, bank)
|
||||||
|
})
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
logger.error(exception, "Caught exception at daily job, shutting down!")
|
logger.error(exception, "Caught exception at CronJob, shutting down!")
|
||||||
await shutdown()
|
await shutdown()
|
||||||
|
} finally {
|
||||||
|
await actual?.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function shutdown(): Promise<void> {
|
async function shutdown(): Promise<void> {
|
||||||
logger.info("Shutting down, Bye!")
|
logger.info("Shutting down, Bye!")
|
||||||
await actual.shutdown()
|
|
||||||
db.close()
|
db.close()
|
||||||
cronJob?.stop()
|
cronJob?.stop()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { describe, it } from "@jest/globals"
|
import { describe, it } from "@jest/globals"
|
||||||
|
|
||||||
import { daily } from "@/main.ts"
|
import { moveTransactions } from "@/main.ts"
|
||||||
import { ActualImpl } from "@/actual.ts"
|
import { ActualImpl } from "@/actual.ts"
|
||||||
import { BankStub } from "./stubs/bankStub.ts"
|
import { BankStub } from "./stubs/bankStub.ts"
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ import { BankStub } from "./stubs/bankStub.ts"
|
|||||||
describe("Main logic of the application", () => {
|
describe("Main logic of the application", () => {
|
||||||
it("should import the transactions to Actual Budget", async () => {
|
it("should import the transactions to Actual Budget", async () => {
|
||||||
const actual = await ActualImpl.init()
|
const actual = await ActualImpl.init()
|
||||||
await daily(actual, new BankStub())
|
await moveTransactions(actual, new BankStub())
|
||||||
await actual.shutdown()
|
await actual.shutdown()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user