bullmq
bullmq copied to clipboard
repeat with immediately:true at second time not work
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,
},
});
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.
But in my mind, it's already removed by removeRepeatableByKey right?
@manast I agree with @suhaotian.
- add repeatable job with
immediately: true-> runs without delay - remove repeatable via removeRepeatableByKey
- 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);
}
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