Skip to content

Commit

Permalink
feat: removed custom repeat strat to utc based cron
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilmhdh committed Jan 31, 2024
1 parent 560f8d4 commit 4b3e4f6
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions backend/src/ee/services/audit-log/audit-log-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ type TAuditLogQueueServiceFactoryDep = {

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 = ({
auditLogDAL,
queueService,
Expand Down Expand Up @@ -73,37 +60,29 @@ export const auditLogQueueServiceFactory = ({
});
});

queueService.start(
QueueName.AuditLogPrune,
async () => {
logger.info("Started audit log pruning");
await auditLogDAL.pruneAuditLog();
// calculate next utc time delay
// const nextPruneTime = getTimeDiffForNextAuditLogPrune();
// await queueService.stopJobById(QueueName.AuditLogPrune, "audit-log-prune");
logger.info("Finished audit log pruning");
},
{
settings: {
repeatStrategy: getTimeDiffForNextAuditLogPrune
}
}
);
queueService.start(QueueName.AuditLogPrune, async () => {
logger.info("Started audit log pruning");
await auditLogDAL.pruneAuditLog();
// calculate next utc time delay
// const nextPruneTime = getTimeDiffForNextAuditLogPrune();
// await queueService.stopJobById(QueueName.AuditLogPrune, "audit-log-prune");
logger.info("Finished audit log pruning");
});

// we are not using repeat because we want to run the in a predictable time of midnight UTC
// repeat has only cron job and every so we do the repeat manually
// we do a repeat cron job in utc timezone at 12 Midnight each day
const startAuditLogPruneJob = async () => {
// clear previous job
await queueService.stopRepeatableJob(
QueueName.AuditLogPrune,
QueueJobs.AuditLogPrune,
{},
{ pattern: "0 0 * * *", utc: true },
QueueName.AuditLogPrune // just a job id
);

await queueService.queue(QueueName.AuditLogPrune, QueueJobs.AuditLogPrune, undefined, {
delay: 5000,
jobId: QueueName.AuditLogPrune,
repeat: {}
repeat: { pattern: "0 0 * * *", utc: true }
});
};

Expand Down

0 comments on commit 4b3e4f6

Please sign in to comment.