chore: change the quota sync to the queue 2

This commit is contained in:
mahyargdz
2025-07-15 16:05:38 +03:30
parent c7c18ebd4c
commit 76b29599da
2 changed files with 55 additions and 7 deletions
+9 -6
View File
@@ -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();
}
}
@@ -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<void> {
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<void> {
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
*/