Skip to content

Commit

Permalink
feat: allow only running one task (importer or validator) at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Mar 14, 2024
1 parent 20cbe2d commit 895a7ad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/tasks/pair-liquidity-info-history-importer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@ type MicroBlock = {
@Injectable()
export class PairLiquidityInfoHistoryImporterService {
constructor(
private tasksService: TasksService,
private mdwClientService: MdwClientService,
private pairService: PairService,
private pairLiquidityInfoHistoryService: PairLiquidityInfoHistoryService,
Expand All @@ -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);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/tasks/pair-liquidity-info-history-validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
Expand All @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
14 changes: 14 additions & 0 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 895a7ad

Please sign in to comment.