From 5e7aa5fb81ffac38fa995f19aebec9639c4e381e Mon Sep 17 00:00:00 2001 From: tcar Date: Thu, 7 Dec 2023 14:38:59 +0100 Subject: [PATCH] stop the cron job; Signed-off-by: tcar --- src/indexer/index.ts | 12 +++++++----- src/indexer/services/monitoringService/index.ts | 5 ++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/indexer/index.ts b/src/indexer/index.ts index 63ef1cfc..39f9b347 100644 --- a/src/indexer/index.ts +++ b/src/indexer/index.ts @@ -19,8 +19,9 @@ import { healthcheckRoute } from "./healthcheck" import { OfacComplianceService } from "./services/ofac" import AccountRepository from "./repository/account" import CoinMarketCapService from "./services/coinmarketcap/coinmarketcap.service" -import { checkTransferStatus, startCronJob } from "./services/monitoringService" +import { checkTransferStatus, getCronJob } from "./services/monitoringService" import { NotificationSender } from "./services/monitoringService/notificationSender" +import { CronJob } from "cron" interface DomainIndexer { listenToEvents(): Promise @@ -54,6 +55,7 @@ init() }) .catch(err => logger.error("Error occurred during database closing: ", err)) + initData.cron.stop() nodeCleanup.uninstall() return false }) @@ -68,7 +70,7 @@ init() logger.error("Error occurred on app initialization: ", reason) }) -async function init(): Promise<{ domainIndexers: Array; app: FastifyInstance }> { +async function init(): Promise<{ domainIndexers: Array; app: FastifyInstance, cron: CronJob }> { const sharedConfig = await getSharedConfig(process.env.SHARED_CONFIG_URL!) const chainAnalysisUrl = process.env.CHAIN_ANALYSIS_URL || "" @@ -100,8 +102,8 @@ async function init(): Promise<{ domainIndexers: Array; app: Fast const notificationSender = new NotificationSender(process.env.SNS_REGION!) const cronTime = process.env.CRON_TIME || "* */10 * * * *" - startCronJob(cronTime, checkTransferStatus, transferRepository, notificationSender) - + const cron = getCronJob(cronTime, checkTransferStatus, transferRepository, notificationSender) + cron.start() for (const domain of domainsToIndex) { const rpcURL = rpcUrlConfig.get(domain.id) if (!rpcURL) { @@ -153,7 +155,7 @@ async function init(): Promise<{ domainIndexers: Array; app: Fast } } - return { domainIndexers, app } + return { domainIndexers, app, cron } } async function insertDomains( diff --git a/src/indexer/services/monitoringService/index.ts b/src/indexer/services/monitoringService/index.ts index 4a38970e..a30b2b32 100644 --- a/src/indexer/services/monitoringService/index.ts +++ b/src/indexer/services/monitoringService/index.ts @@ -42,13 +42,12 @@ export async function checkTransferStatus(transferRepository: TransferRepository } } -export function startCronJob(cronTime: string | Date | DateTime, fn: Function, ...args: Parameters): void { - const cronJob = new CronJob(cronTime, () => { +export function startCronJob(cronTime: string | Date | DateTime, fn: Function, ...args: Parameters): CronJob { + return new CronJob(cronTime, () => { try { fn(...args) } catch (err) { logger.error("Error while executing cron job function", err) } }) - cronJob.start() }