Skip to content

Commit

Permalink
fix(1165): correct condition for cron job runs
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Oct 18, 2024
1 parent d9f9b34 commit bddcffe
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/utils/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const getNextJob = async (state: jobState, exclude: string[]) => {
const iterateJobQueue = async (state: jobState, cb: (job: Job) => Promise<void>): Promise<number> => {
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);
Expand All @@ -63,6 +64,7 @@ const iterateJobQueue = async (state: jobState, cb: (job: Job) => Promise<void>)
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}`);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bddcffe

Please sign in to comment.