remove specific repeatable jobs in bullmq (with jobid)

3.2k Views Asked by At

I need to remove specific individual job from queue. actual case is I've added a job into queue and then in some API call I need to remove that individual job from the queue. so, It won't be repeat in future.

add:

const job = await this.locationQueue.add('alert', { handle, author, data }, { repeat: { every: 60000 } });

remove:

await this.locationQueue.removeRepeatable('alert', { jobId: request.jobKey, every: 60000 });

Where request.jobKey is job.id from add.

3

There are 3 best solutions below

2
On

You need to specify the same jobId when you adding it to the queue, as far as I know.

const job = await this.locationQueue.add('alert', { handle, author, data, jobId: 'some job unique job id'}, { repeat: { every: 60000 } });
0
On

When adding a new job in the queue, you need to specify a jobId for your situation, as mentioned by @Ayzrian's answer.

But that jobId should be provided by you, and not the one which BullMQ generates for itself.

The id of a job that BullMQ generates is for later use, when job has already been queued.

The jobId that you'll provide when queuing the repeatable job should be the same which you'll use while removing the same repeatable job (along with other parameters, as mentioned in BullMQ docs).

0
On

Firstly you need to create your job with a specified jobId:

await queue.add('job-name', user.id, {
  repeat: { every: 1000 },
  jobId: 'job-id',
});

Then you can delete it like this by combining job name, id and delay:

await queue.removeRepeatableByKey(
  'job-name:job-id::1000',
);

Note that there are 2 colons specified after delay.