From 76b29599da1ac9cb8fdc9e4fb96442377db2080a Mon Sep 17 00:00:00 2001 From: mahyargdz Date: Tue, 15 Jul 2025 16:05:38 +0330 Subject: [PATCH] chore: change the quota sync to the queue 2 --- src/modules/quota-sync/quota-sync.module.ts | 15 +++--- .../quota-sync/services/quota-sync.service.ts | 47 ++++++++++++++++++- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/modules/quota-sync/quota-sync.module.ts b/src/modules/quota-sync/quota-sync.module.ts index 1b8ede2..21417e6 100644 --- a/src/modules/quota-sync/quota-sync.module.ts +++ b/src/modules/quota-sync/quota-sync.module.ts @@ -1,5 +1,5 @@ import { BullModule } from "@nestjs/bullmq"; -import { Module } from "@nestjs/common"; +import { Module, OnModuleInit } from "@nestjs/common"; import { QUOTA_SYNC_QUEUE } from "./constants"; import { QuotaSyncProcessor } from "./queue/quota-sync.processor"; @@ -12,13 +12,16 @@ import { MailServerModule } from "../mail-server/mail-server.module"; BullModule.registerQueue({ name: QUOTA_SYNC_QUEUE.QUEUE_NAME, prefix: QUOTA_SYNC_QUEUE.QUEUE_PREFIX, - defaultJobOptions: { - removeOnComplete: true, - removeOnFail: true, - }, }), ], providers: [QuotaSyncService, QuotaSyncProcessor], exports: [QuotaSyncService], }) -export class QuotaSyncModule {} +export class QuotaSyncModule implements OnModuleInit { + constructor(private readonly quotaSyncService: QuotaSyncService) {} + + async onModuleInit() { + // Setup the cron job when the module initializes + await this.quotaSyncService.setupCronJob(); + } +} diff --git a/src/modules/quota-sync/services/quota-sync.service.ts b/src/modules/quota-sync/services/quota-sync.service.ts index 2477160..f0b50fb 100644 --- a/src/modules/quota-sync/services/quota-sync.service.ts +++ b/src/modules/quota-sync/services/quota-sync.service.ts @@ -23,11 +23,56 @@ export class QuotaSyncService { private readonly logger = new Logger(QuotaSyncService.name); constructor( - @InjectQueue(QUOTA_SYNC_QUEUE.QUEUE_NAME) private readonly quotaSyncQueue: Queue, private readonly wildDuckUsersService: WildDuckUsersService, private readonly em: EntityManager, + @InjectQueue(QUOTA_SYNC_QUEUE.QUEUE_NAME) private readonly quotaSyncQueue: Queue, ) {} + /** + * Setup repeatable cron job to run comprehensive quota sync every hour + */ + async setupCronJob(): Promise { + try { + // Remove any existing cron jobs for this job type + await this.quotaSyncQueue.removeJobScheduler(QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB); + + // Add new repeatable job + await this.quotaSyncQueue.add( + QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB, + {}, + { + removeOnComplete: true, + removeOnFail: false, + attempts: 3, + backoff: { + type: "exponential", + delay: 2000, + }, + repeat: { + pattern: "0 * * * *", // Every hour at minute 0 (cron pattern) + }, + }, + ); + + 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); + throw error; + } + } + + /** + * Remove the repeatable cron job + */ + async removeCronJob(): Promise { + try { + await this.quotaSyncQueue.removeJobScheduler(QUOTA_SYNC_QUEUE.SYNC_COMPREHENSIVE_QUOTA_JOB); + this.logger.log("Quota sync cron job removed"); + } catch (error) { + this.logger.error("Failed to remove quota sync cron job", error); + } + } + /** * Add a job to sync quota usage for all users */