How to abort a particular task in bull queue?
Asked Answered
B

1

5

According to this package https://github.com/OptimalBits/bull is it possible, to abort a certain task in the "waiting queue"? My use-case is as follows:

I have a mongodb collection "users" and a collection "friendship" where I store name and avatar of both users. So I only need one query to get friendlist of a certain user. When a user changes his avatar, I have to update all documents within this user in "friendship" collection. This is a performance-uncritical operation since I want it to do in background and consistency is not important for this use-case. But when a User updates his avatar several times in a short time span, I want to cancel all referencing old tasks (for updating the friendship collection) except the newest. Is this with bull possible?

Thanks in advance, I would appreciate every information about that.

Burnley answered 29/11, 2017 at 11:13 Comment(0)
I
8

Looking at the Bull reference you will find that there is a Job.remove() method. Since you haven't posted any code I could only guess how it looks like. Hence I have described what you could do.

However what you have to do is to store the Promise<Job> which will be returned by Queue.add() for instance in a Map<string, Map<string, Promise<Job>>. String would be the _id of your user and Promise<Job>[] is an array containing all the queued jobs for a specific user. Once a Job has been resolved (you can await the resolved job with Job.finished()) you need to remove the Promise from your Map.

Whenever a user changes his avatar you could then look into your Map if you need to remove any jobs. The value in the above mentioned Map is another Map (key is a string, which represents the JobId) which easily allows you to remove Jobs by JobId. That may sound a bit complex, but don't be afraid - if you understand how Maps work it shouldn't be a problem :-).

Isabelisabelita answered 29/11, 2017 at 12:55 Comment(1)
@Isabelisabelita can you answer this question #56257109 ?Leyden

© 2022 - 2024 — McMap. All rights reserved.