bull
bull copied to clipboard
Update scheduled job?
User scheduled post for tomorrow:
postQueue.add({ postId: 1 }, { delay: 60 * 60 * 24 });
in 2 hour he changed mind and want to reschedule this post after 3 days.
As I saw in documentation, queue has getJob method which finds job with Id but we want to find job with postId. Also query all jobs will be slow, if there will huge amount of jobs in queue.
How to find this job and update with correct delay?
You will need to either save the jobIds in some external database, or if the amount of delayed jobs is not huge use a graphical front end such as https://taskforce.sh to find the job ID.
Is there a way to update the delay on a scheduled job? Currrently Job.update
only lets you update the job data
.
@amitav13 no, not possible atm.
Would something like this be a workaround?
/**
* Update the delay for a job by canceling it and creating a new one.
* @param {Bull.Queue} queue
* @param {Bull.Job} job
* @param {number} newDelayMs
*/
async function updateJobDelay(queue, job, newDelayMs) {
// @ts-ignore
queue.removeJobs(job.id);
const options = {
...job.opts,
delay: newDelayMs,
};
queue.add(job.data, options);
}
@amitav13 it will work most of the time but it is not robust, many edge cases can happen here, for example, the delayed job maybe is already running, or your process crashes between the call to removeJob and queue.add so you end without any job, etc.
Agreed, I'm new to bull so I imagine there are a lot of scenarios not covered in that snippet. Hope we can get a more robust one directly from the lib soon :)
Till then, I'll leave my updated snippet here for anyone else looking:
/**
* Update the delay for a job by canceling it and creating a new one.
*
* https://github.com/OptimalBits/bull/issues/1857#issuecomment-807974136
*
* @param {Bull.Queue} queue
* @param {Bull.Job} job
* @param {number} newDelayMs
*/
async function updateJobDelay(queue, job, newDelayMs) {
if (await job.isDelayed) {
// @ts-ignore
await queue.removeJobs(job.id);
const options = {
...job.opts,
delay: newDelayMs,
};
await queue.add(job.data, options);
}
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I still hope we can get this directly from bull!