how to remove a delayed job from bull js?

11.9k views Asked by At

I am new to Bull and my use case is to run a job after 10 sec, for that, I am using the below code

  const options = {
    delay: 10000, // in ms
    jobId: myCustomUUID,
  };

  myQueue.add(someRandomData, options);

after adding it to the queue, now after a few sec let's say 4 sec, i want to remove the job from the queue as it is no longer required due to some condition, how can I achieve it. I know there is job.remove(). but how to use it for a given jobId. can someone please help me with it.

3

There are 3 answers

0
Xayrulloh Abduvohidov On

I think this is best solution:

this.queue.add('key', {data}, {removeOnComplete: true, removeOnFail: true})
0
Emiliano Barboza On

to do that automatically you can setup on this way: https://github.com/OptimalBits/bull/blob/HEAD/REFERENCE.md#user-content-queueadd

Assuming you have a queue and some model.

const jobOptions = {
        removeOnComplete: true,
        removeOnFail: true
    }
this.queue.add(model, jobOptions)
0
Rafiq On

first, give a job id when you are adding the queue.

  await promotionEndQueue.add(
          payload,
          {
            delay: delay,
            jobId: `${data.id}`,
          }
        );

if want to remove the job, grab it by the id, it was registered with.

// first find the job by Id
const job = await promotionEndQueue.getJob(data.id);
// then remove the job
await job?.remove();