bull icon indicating copy to clipboard operation
bull copied to clipboard

Update scheduled job?

Open VakoNonikashvili opened this issue 3 years ago • 8 comments

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?

VakoNonikashvili avatar Sep 14 '20 22:09 VakoNonikashvili

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.

manast avatar Sep 15 '20 06:09 manast

Is there a way to update the delay on a scheduled job? Currrently Job.update only lets you update the job data.

amitav13 avatar Mar 26 '21 05:03 amitav13

@amitav13 no, not possible atm.

manast avatar Mar 26 '21 06:03 manast

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 avatar Mar 26 '21 08:03 amitav13

@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.

manast avatar Mar 26 '21 08:03 manast

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);
  }
}

amitav13 avatar Mar 26 '21 12:03 amitav13

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.

stale[bot] avatar Jul 12 '21 13:07 stale[bot]

I still hope we can get this directly from bull!

amitav13 avatar Jul 12 '21 13:07 amitav13