bullmq icon indicating copy to clipboard operation
bullmq copied to clipboard

repeat with immediately:true at second time not work

Open suhaotian opened this issue 2 years ago • 3 comments

Code with below, first time will run immediately. but restart the server, add the same job to queue, no more effect with second time run:

const jobs = await queue.getRepeatableJobs();
// remove repeat jobs
await Promise.all(
  jobs.map(i => {
    return queue.removeRepeatableByKey(i.key);
  }),
);
await queue.add('task-name', {}, {
 repeat: {
      every: 1000 * 60 * 60 * 1.6,
      immediately: true,
    },
});

suhaotian avatar Dec 27 '22 09:12 suhaotian

I think this works as designed. Since you are adding the same repeatable job, only the first time it will be immediate, then it will just repeat according to the "every" option.

manast avatar Jan 10 '23 14:01 manast

But in my mind, it's already removed by removeRepeatableByKey right?

suhaotian avatar Jan 13 '23 23:01 suhaotian

@manast I agree with @suhaotian.

  1. add repeatable job with immediately: true -> runs without delay
  2. remove repeatable via removeRepeatableByKey
  3. re-add repeatable job with immediately: true -> runs with delay

It's not intuitive or expected imo

--

Edit:

Was able to achieve the desired functionality by retrying if the added job's state is completed or failed:

const job = await queue.add('task-name', {}, {
 repeat: {
      every: 1000 * 60 * 60 * 1.6,
      immediately: true,
    },
});

const state = await job.getState();
if (state === 'completed' || state === 'failed') {
  await job.retry(state);
}

tjhiggins avatar Oct 23 '23 19:10 tjhiggins

I created a test to specifically cover this case. It works perfectly, the problem is that you are probably getting already the second iteration of the job before you remove the job scheduler. Probably by having a larger every setting you will not be able to reproduce it anymore: https://github.com/taskforcesh/bullmq/pull/2805

manast avatar Oct 07 '24 17:10 manast