feat: removed custom repeat strat to utc based cron

This commit is contained in:
Akhil Mohan
2024-01-31 22:51:04 +05:30
parent 560f8d4a9b
commit 4b3e4f6a1e
@@ -15,19 +15,6 @@ type TAuditLogQueueServiceFactoryDep = {
export type TAuditLogQueueServiceFactory = ReturnType<typeof auditLogQueueServiceFactory>; export type TAuditLogQueueServiceFactory = ReturnType<typeof auditLogQueueServiceFactory>;
const getTimeDiffForNextAuditLogPrune = (mills: number) => {
const today = new Date(mills);
// Get UTC midnight timestamp for today
const nextUtcMidnight = new Date(Date.UTC(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0));
// Check if we have already passed UTC midnight today
if (today.getTime() >= nextUtcMidnight.getTime()) {
// Add one day to get the timestamp for tomorrow's UTC midnight
nextUtcMidnight.setDate(nextUtcMidnight.getDate() + 1);
}
return nextUtcMidnight.getTime();
};
export const auditLogQueueServiceFactory = ({ export const auditLogQueueServiceFactory = ({
auditLogDAL, auditLogDAL,
queueService, queueService,
@@ -73,37 +60,29 @@ export const auditLogQueueServiceFactory = ({
}); });
}); });
queueService.start( queueService.start(QueueName.AuditLogPrune, async () => {
QueueName.AuditLogPrune, logger.info("Started audit log pruning");
async () => { await auditLogDAL.pruneAuditLog();
logger.info("Started audit log pruning"); // calculate next utc time delay
await auditLogDAL.pruneAuditLog(); // const nextPruneTime = getTimeDiffForNextAuditLogPrune();
// calculate next utc time delay // await queueService.stopJobById(QueueName.AuditLogPrune, "audit-log-prune");
// const nextPruneTime = getTimeDiffForNextAuditLogPrune(); logger.info("Finished audit log pruning");
// await queueService.stopJobById(QueueName.AuditLogPrune, "audit-log-prune"); });
logger.info("Finished audit log pruning");
},
{
settings: {
repeatStrategy: getTimeDiffForNextAuditLogPrune
}
}
);
// we are not using repeat because we want to run the in a predictable time of midnight UTC // we do a repeat cron job in utc timezone at 12 Midnight each day
// repeat has only cron job and every so we do the repeat manually
const startAuditLogPruneJob = async () => { const startAuditLogPruneJob = async () => {
// clear previous job // clear previous job
await queueService.stopRepeatableJob( await queueService.stopRepeatableJob(
QueueName.AuditLogPrune, QueueName.AuditLogPrune,
QueueJobs.AuditLogPrune, QueueJobs.AuditLogPrune,
{}, { pattern: "0 0 * * *", utc: true },
QueueName.AuditLogPrune // just a job id QueueName.AuditLogPrune // just a job id
); );
await queueService.queue(QueueName.AuditLogPrune, QueueJobs.AuditLogPrune, undefined, { await queueService.queue(QueueName.AuditLogPrune, QueueJobs.AuditLogPrune, undefined, {
delay: 5000, delay: 5000,
jobId: QueueName.AuditLogPrune, jobId: QueueName.AuditLogPrune,
repeat: {} repeat: { pattern: "0 0 * * *", utc: true }
}); });
}; };