根据这个包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 :-).
更多推荐
发布评论