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
@@ -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
*/