From 63ddd394c946bd08789243a8ee5ee0048ab83b6e Mon Sep 17 00:00:00 2001 From: mahyargdz Date: Tue, 15 Jul 2025 16:25:25 +0330 Subject: [PATCH] update: the quota sync service --- .../quota-sync/queue/quota-sync.processor.ts | 4 ++-- .../quota-sync/services/quota-sync.service.ts | 24 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/modules/quota-sync/queue/quota-sync.processor.ts b/src/modules/quota-sync/queue/quota-sync.processor.ts index 5e4e3e3..83e6317 100644 --- a/src/modules/quota-sync/queue/quota-sync.processor.ts +++ b/src/modules/quota-sync/queue/quota-sync.processor.ts @@ -90,10 +90,10 @@ export class QuotaSyncProcessor extends WorkerProcessor { } private async handleSyncComprehensiveQuota(_job: Job): Promise { - this.logger.log("Processing comprehensive quota sync"); + this.logger.debug("Processing comprehensive quota sync ........................."); await this.quotaSyncService.syncAllQuotas(); - this.logger.log("Successfully completed comprehensive quota sync"); + this.logger.debug("Successfully completed comprehensive quota sync ........................."); } private logJobSuccess(job: Job, startTime: number): void { diff --git a/src/modules/quota-sync/services/quota-sync.service.ts b/src/modules/quota-sync/services/quota-sync.service.ts index f0b50fb..6fe1eb0 100644 --- a/src/modules/quota-sync/services/quota-sync.service.ts +++ b/src/modules/quota-sync/services/quota-sync.service.ts @@ -34,10 +34,11 @@ export class QuotaSyncService { async setupCronJob(): Promise { try { // Remove any existing cron jobs for this job type - await this.quotaSyncQueue.removeJobScheduler(QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB); + // Get all repeatable jobs and remove the one with matching name + await this.removeCronJob(); // Add new repeatable job - await this.quotaSyncQueue.add( + const job = await this.quotaSyncQueue.add( QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB, {}, { @@ -49,11 +50,11 @@ export class QuotaSyncService { delay: 2000, }, repeat: { - pattern: "0 * * * *", // Every hour at minute 0 (cron pattern) + pattern: "0/1 * * * *", // Every hour at minute 0 (cron pattern) }, }, ); - + this.logger.debug(`Added new cron job with ID: ${job.id}`); this.logger.log("Quota sync cron job scheduled to run every hour"); } catch (error) { this.logger.error("Failed to setup quota sync cron job", error); @@ -66,8 +67,19 @@ export class QuotaSyncService { */ async removeCronJob(): Promise { try { - await this.quotaSyncQueue.removeJobScheduler(QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB); - this.logger.log("Quota sync cron job removed"); + // Remove any existing cron jobs for this job type + // Get all repeatable jobs and remove the one with matching name + const repeatableJobs = await this.quotaSyncQueue.getJobSchedulers(); + const existingJob = repeatableJobs.filter((job) => job.name === QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB); + + if (existingJob.length > 0) { + for (const job of existingJob) { + const removed = await this.quotaSyncQueue.removeJobScheduler(job.key); + this.logger.debug(`Removed existing cron job with key ${job.key}: ${removed}`); + } + } else { + this.logger.debug("No existing cron job found to remove"); + } } catch (error) { this.logger.error("Failed to remove quota sync cron job", error); }