From bddcffe5f1f6c9b407488e243d125139dc1c7069 Mon Sep 17 00:00:00 2001 From: Maksim Sviridov Date: Wed, 9 Oct 2024 14:01:35 +0300 Subject: [PATCH] fix(1165): correct condition for cron job runs --- src/utils/worker/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/utils/worker/index.ts b/src/utils/worker/index.ts index 43fc097a2..c2b994549 100644 --- a/src/utils/worker/index.ts +++ b/src/utils/worker/index.ts @@ -44,6 +44,7 @@ const getNextJob = async (state: jobState, exclude: string[]) => { const iterateJobQueue = async (state: jobState, cb: (job: Job) => Promise): Promise => { const watchedIds: string[] = []; + // eslint-disable-next-line no-constant-condition while (true) { // eslint-disable-next-line no-await-in-loop const job = await getNextJob(state, watchedIds); @@ -63,6 +64,7 @@ const iterateJobQueue = async (state: jobState, cb: (job: Job) => Promise) const worker = async () => { try { const completedCount = await iterateJobQueue(jobState.completed, async (job) => { + log(`completed: ${job.id} - ${job.kind}`); setTimeout(async () => { if (job.cron) { log(`plan cron ${job.id}`); @@ -93,14 +95,21 @@ const worker = async () => { currentDate: new Date(job.updatedAt), }); - if (Number(interval.next().toDate()) > Date.now() && !job.force) { + const nextCronIntervalInMinutes = Math.floor(Number(interval.next().toDate()) / 1000 / 60); + const nowToMinutes = Math.floor(Date.now() / 1000 / 60); + + if (nextCronIntervalInMinutes > nowToMinutes && !job.force) { await planJob(); return; } } - if (job.delay && Date.now() - new Date(job.createdAt).valueOf() < job.delay) { + if ( + job.delay && + (Date.now() - new Date(job.createdAt).valueOf() < job.delay || + Date.now() - new Date(job.updatedAt).valueOf() < job.delay) + ) { await planJob(); return;