diff --git a/blockchain-watcher/src/domain/actions/RunPollingJob.ts b/blockchain-watcher/src/domain/actions/RunPollingJob.ts index 29880eac..868af0ac 100644 --- a/blockchain-watcher/src/domain/actions/RunPollingJob.ts +++ b/blockchain-watcher/src/domain/actions/RunPollingJob.ts @@ -1,7 +1,8 @@ -import { setTimeout } from "timers/promises"; -import winston from "winston"; -import { Handler } from "../entities"; import { StatRepository } from "../repositories"; +import { performance } from "perf_hooks"; +import { setTimeout } from "timers/promises"; +import { Handler } from "../entities"; +import winston from "winston"; const DEFAULT_INTERVAL = 1_000; @@ -19,14 +20,15 @@ export abstract class RunPollingJob { constructor(id: string, statRepo?: StatRepository, interval: number = DEFAULT_INTERVAL) { this.interval = interval; - this.id = id; - this.running = true; this.statRepo = statRepo; + this.running = true; + this.id = id; } public async run(handlers: Handler[]): Promise { this.logger.info("[run] Starting polling job"); await this.preHook(); + while (this.running) { if (!(await this.hasNext())) { this.logger.info("[run] Finished processing"); @@ -38,8 +40,16 @@ export abstract class RunPollingJob { try { this.report(); + + const jobStartTime = performance.now(); + items = await this.get(); await Promise.all(handlers.map((handler) => handler(items))); + + const jobEndTime = performance.now(); + const jobExecutionTime = Number(((jobEndTime - jobStartTime) / 1000).toFixed(2)); + + this.statRepo?.measure("job_execution_time", jobExecutionTime, { job: this.id }); this.statRepo?.count("job_items_total", { id: this.id }, items.length); } catch (e: Error | any) { this.logger.error("[run] Error processing items", e); diff --git a/blockchain-watcher/src/domain/repositories.ts b/blockchain-watcher/src/domain/repositories.ts index caa6d158..7142b1e3 100644 --- a/blockchain-watcher/src/domain/repositories.ts +++ b/blockchain-watcher/src/domain/repositories.ts @@ -98,7 +98,7 @@ export interface MetadataRepository { export interface StatRepository { count(id: string, labels: Record, increase?: number): void; - measure(id: string, value: bigint, labels: Record): void; + measure(id: string, value: bigint | number, labels: Record): void; report: () => Promise; }