diff --git a/src/core/services/cron.ts b/src/core/services/cron.ts index a5b82dfa3..eca0b1447 100644 --- a/src/core/services/cron.ts +++ b/src/core/services/cron.ts @@ -18,12 +18,14 @@ import { CoreApp } from '@services/app'; import { CoreConfig } from '@services/config'; import { CoreUtils } from '@services/utils/utils'; import { CoreConstants } from '@/core/constants'; -import { SQLiteDB } from '@classes/sqlitedb'; import { CoreError } from '@classes/errors/error'; import { makeSingleton } from '@singletons'; import { CoreLogger } from '@singletons/logger'; import { APP_SCHEMA, CRON_TABLE_NAME, CronDBEntry } from '@services/database/cron'; +import { asyncInstance } from '../utils/async-instance'; +import { CoreDatabaseTable } from '@classes/database/database-table'; +import { CoreDatabaseCachingStrategy, CoreDatabaseTableProxy } from '@classes/database/database-table-proxy'; /* * Service to handle cron processes. The registered processes will be executed every certain time. @@ -39,13 +41,9 @@ export class CoreCronDelegateService { protected logger: CoreLogger; protected handlers: { [s: string]: CoreCronHandler } = {}; protected queuePromise: Promise = Promise.resolve(); - - // Variables for DB. - protected appDB: Promise; - protected resolveAppDB!: (appDB: SQLiteDB) => void; + protected table = asyncInstance>(); constructor() { - this.appDB = new Promise(resolve => this.resolveAppDB = resolve); this.logger = CoreLogger.getInstance('CoreCronDelegate'); } @@ -59,7 +57,15 @@ export class CoreCronDelegateService { // Ignore errors. } - this.resolveAppDB(CoreApp.getDB()); + const table = new CoreDatabaseTableProxy( + { cachingStrategy: CoreDatabaseCachingStrategy.Eager }, + CoreApp.getDB(), + CRON_TABLE_NAME, + ); + + await table.initialize(); + + this.table.setInstance(table); } /** @@ -238,11 +244,10 @@ export class CoreCronDelegateService { * @return Promise resolved with the handler's last execution time. */ protected async getHandlerLastExecutionTime(name: string): Promise { - const db = await this.appDB; const id = this.getHandlerLastExecutionId(name); try { - const entry = await db.getRecord(CRON_TABLE_NAME, { id }); + const entry = await this.table.getOneByPrimaryKey({ id }); const time = Number(entry.value); @@ -397,14 +402,13 @@ export class CoreCronDelegateService { * @return Promise resolved when the execution time is saved. */ protected async setHandlerLastExecutionTime(name: string, time: number): Promise { - const db = await this.appDB; const id = this.getHandlerLastExecutionId(name); const entry = { id, value: time, }; - await db.insertRecord(CRON_TABLE_NAME, entry); + await this.table.insert(entry); } /**