MOBILE-3981 core: Optimize cron table

main
Noel De Martin 2022-02-10 15:55:03 +01:00
parent cc006bad26
commit 18c48940bf
1 changed files with 15 additions and 11 deletions

View File

@ -18,12 +18,14 @@ import { CoreApp } from '@services/app';
import { CoreConfig } from '@services/config'; import { CoreConfig } from '@services/config';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreConstants } from '@/core/constants'; import { CoreConstants } from '@/core/constants';
import { SQLiteDB } from '@classes/sqlitedb';
import { CoreError } from '@classes/errors/error'; import { CoreError } from '@classes/errors/error';
import { makeSingleton } from '@singletons'; import { makeSingleton } from '@singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { APP_SCHEMA, CRON_TABLE_NAME, CronDBEntry } from '@services/database/cron'; 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. * 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 logger: CoreLogger;
protected handlers: { [s: string]: CoreCronHandler } = {}; protected handlers: { [s: string]: CoreCronHandler } = {};
protected queuePromise: Promise<void> = Promise.resolve(); protected queuePromise: Promise<void> = Promise.resolve();
protected table = asyncInstance<CoreDatabaseTable<CronDBEntry>>();
// Variables for DB.
protected appDB: Promise<SQLiteDB>;
protected resolveAppDB!: (appDB: SQLiteDB) => void;
constructor() { constructor() {
this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
this.logger = CoreLogger.getInstance('CoreCronDelegate'); this.logger = CoreLogger.getInstance('CoreCronDelegate');
} }
@ -59,7 +57,15 @@ export class CoreCronDelegateService {
// Ignore errors. // Ignore errors.
} }
this.resolveAppDB(CoreApp.getDB()); const table = new CoreDatabaseTableProxy<CronDBEntry>(
{ 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. * @return Promise resolved with the handler's last execution time.
*/ */
protected async getHandlerLastExecutionTime(name: string): Promise<number> { protected async getHandlerLastExecutionTime(name: string): Promise<number> {
const db = await this.appDB;
const id = this.getHandlerLastExecutionId(name); const id = this.getHandlerLastExecutionId(name);
try { try {
const entry = await db.getRecord<CronDBEntry>(CRON_TABLE_NAME, { id }); const entry = await this.table.getOneByPrimaryKey({ id });
const time = Number(entry.value); const time = Number(entry.value);
@ -397,14 +402,13 @@ export class CoreCronDelegateService {
* @return Promise resolved when the execution time is saved. * @return Promise resolved when the execution time is saved.
*/ */
protected async setHandlerLastExecutionTime(name: string, time: number): Promise<void> { protected async setHandlerLastExecutionTime(name: string, time: number): Promise<void> {
const db = await this.appDB;
const id = this.getHandlerLastExecutionId(name); const id = this.getHandlerLastExecutionId(name);
const entry = { const entry = {
id, id,
value: time, value: time,
}; };
await db.insertRecord(CRON_TABLE_NAME, entry); await this.table.insert(entry);
} }
/** /**