Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scheduled job? #1857

Open
VakoNonikashvili opened this issue Sep 14, 2020 · 8 comments
Open

Update scheduled job? #1857

VakoNonikashvili opened this issue Sep 14, 2020 · 8 comments

Comments

@VakoNonikashvili
Copy link
Contributor

VakoNonikashvili commented Sep 14, 2020

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?

@manast
Copy link
Member

manast commented Sep 15, 2020

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.

@amitav13
Copy link

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

@manast
Copy link
Member

manast commented Mar 26, 2021

@amitav13 no, not possible atm.

@amitav13
Copy link

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

@manast
Copy link
Member

manast commented Mar 26, 2021

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

@amitav13
Copy link

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

@stale
Copy link

stale bot commented Jul 12, 2021

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 stale bot added the wontfix label Jul 12, 2021
@amitav13
Copy link

I still hope we can get this directly from bull!

@stale stale bot removed the wontfix label Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants