如何中止公牛队列中的特定任务?(How to abort a particular task in bull queue?)

编程入门 行业动态 更新时间:2024-10-25 04:22:24
如何中止公牛队列中的特定任务?(How to abort a particular task in bull queue?)

根据这个包https://github.com/OptimalBits/bull是否有可能在“等待队列”中中止某个任务? 我的用例如下:

我有一个mongodb集合“用户”和一个集合“友谊”,我存储两个用户的名称和头像。 所以我只需要一个查询来获取某个用户的好友列表。 当用户更改他的头像时,我必须在“友情”集合中更新此用户中的所有文档。 这是一个性能不关键的操作,因为我希望它在后台执行,并且对于此用例而言,一致性并不重要。 但是当用户在短时间内多次更新他的头像时,我想取消所有引用的旧任务(用于更新友情收集),除了最新的。 这与公牛可能吗?

在此先感谢,我将非常感谢您的每一个信息。

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.

最满意答案

看看Bull引用,你会发现有一个Job.remove()方法。 既然你没有发布任何代码,我只能猜测它的样子。 因此我已经描述了你可以做的事情。

然而,你必须做的是存储Promise<Job> ,它将由Queue.add()返回,例如在Map<string, Map<string, Promise<Job>> 。 String将是您的用户的_id , Promise<Job>[]是一个包含特定用户的所有排队作业的数组。 一旦Job已经解决(您可以使用Job.finished()等待已解决的作业),您需要从Map中删除Promise。

每当用户更改自己的头像时,如果需要删除任何工作,则可以查看自己的地图。 上面提到的Map中的值是另一个Map(key是一个字符串,代表JobId ),它可以让你轻松地通过JobId删除Jobs。 这可能听起来有点复杂,但不要害怕 - 如果你了解地图如何工作它应该不是一个问题:-)。

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 :-).

更多推荐

本文发布于:2023-07-19 01:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   公牛   abort   queue   bull

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!