diff --git a/src/tasks/pair-liquidity-info-history-importer.service.ts b/src/tasks/pair-liquidity-info-history-importer.service.ts index 1655350..5d57722 100644 --- a/src/tasks/pair-liquidity-info-history-importer.service.ts +++ b/src/tasks/pair-liquidity-info-history-importer.service.ts @@ -12,6 +12,7 @@ import { PairLiquidityInfoHistoryErrorService } from '../database/pair-liquidity import { getClient } from '../lib/contracts'; import { Cron, CronExpression } from '@nestjs/schedule'; import { ContractLog } from '../clients/mdw-client.model'; +import { TasksService } from './tasks.service'; type MicroBlock = { hash: MicroBlockHash; @@ -22,6 +23,7 @@ type MicroBlock = { @Injectable() export class PairLiquidityInfoHistoryImporterService { constructor( + private tasksService: TasksService, private mdwClientService: MdwClientService, private pairService: PairService, private pairLiquidityInfoHistoryService: PairLiquidityInfoHistoryService, @@ -34,19 +36,17 @@ export class PairLiquidityInfoHistoryImporterService { readonly WITHIN_HOURS_TO_SKIP_IF_ERROR = 6; - private isSyncRunning: boolean = false; - @Cron(CronExpression.EVERY_5_MINUTES) async runTask() { try { - if (!this.isSyncRunning) { - this.isSyncRunning = true; + if (!this.tasksService.isRunning) { + this.tasksService.setIsRunning(true); await this.syncPairLiquidityInfoHistory(); - this.isSyncRunning = false; + this.tasksService.setIsRunning(false); } } catch (error) { this.logger.error(`Sync failed. ${error}`); - this.isSyncRunning = false; + this.tasksService.setIsRunning(false); } } diff --git a/src/tasks/pair-liquidity-info-history-validator.service.ts b/src/tasks/pair-liquidity-info-history-validator.service.ts index fdbe71e..7499ae1 100644 --- a/src/tasks/pair-liquidity-info-history-validator.service.ts +++ b/src/tasks/pair-liquidity-info-history-validator.service.ts @@ -5,12 +5,14 @@ import { uniq } from 'lodash'; import { Cron } from '@nestjs/schedule'; import { getClient } from '../lib/contracts'; import { MicroBlockHash } from '../lib/utils'; +import { TasksService } from './tasks.service'; const EVERY_5_MINUTES_STARTING_AT_02_30 = '30 2-57/5 * * * *'; @Injectable() export class PairLiquidityInfoHistoryValidatorService { constructor( + private tasksService: TasksService, private mdwClientService: MdwClientService, private pairLiquidityInfoHistoryService: PairLiquidityInfoHistoryService, ) {} @@ -19,19 +21,17 @@ export class PairLiquidityInfoHistoryValidatorService { PairLiquidityInfoHistoryValidatorService.name, ); - private isValidationRunning: boolean = false; - @Cron(EVERY_5_MINUTES_STARTING_AT_02_30) async runTask() { try { - if (!this.isValidationRunning) { - this.isValidationRunning = true; + if (!this.tasksService.isRunning) { + this.tasksService.setIsRunning(true); await this.validatePairLiquidityInfoHistory(); - this.isValidationRunning = false; + this.tasksService.setIsRunning(false); } } catch (error) { - this.isValidationRunning = false; this.logger.error(`Validation failed. ${error}`); + this.tasksService.setIsRunning(false); } } diff --git a/src/tasks/tasks.module.ts b/src/tasks/tasks.module.ts index a11f35d..c6cfbfb 100644 --- a/src/tasks/tasks.module.ts +++ b/src/tasks/tasks.module.ts @@ -4,12 +4,14 @@ import { DatabaseModule } from '../database/database.module'; import { ClientsModule } from '../clients/clients.module'; import { ScheduleModule } from '@nestjs/schedule'; import { PairLiquidityInfoHistoryValidatorService } from './pair-liquidity-info-history-validator.service'; +import { TasksService } from './tasks.service'; @Module({ imports: [ClientsModule, DatabaseModule, ScheduleModule.forRoot()], providers: [ PairLiquidityInfoHistoryImporterService, PairLiquidityInfoHistoryValidatorService, + TasksService, ], }) export class TasksModule {} diff --git a/src/tasks/tasks.service.ts b/src/tasks/tasks.service.ts new file mode 100644 index 0000000..436ac5a --- /dev/null +++ b/src/tasks/tasks.service.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class TasksService { + private _isRunning = false; + + get isRunning(): boolean { + return this._isRunning; + } + + setIsRunning(isRunning: boolean): void { + this._isRunning = isRunning; + } +}