![](/img/trans.png)
[英]How to specify a default queue to use for all jobs with Resque in Rails?
[英]How to shuffle jobs in a Resque queue?
我有一个名为 check_integrity 的队列,其中有很多工作。 当我为它运行一个工人时,它首先需要工作。 是否可以对特定队列中的作业进行洗牌? 我需要工人随机工作。 请帮忙。
谢谢。
关于此问题的 go 的一种方法是从队列中弹出条目,将它们分批,重新洗牌,然后重新插入它们:
key = "resque:queue:bulk"
total = Redis.current.llen(key)
batch_size = 5_000 # any value that is good enough for you
batch = []
total.times do |i|
entry = Redis.current.lpop(key)
batch << entry
if batch.size == batch_size || i.succ == total
puts "re-inserting batch..."
Redis.current.rpush key, batch.shuffle
batch = []
end
end
当您错误地将多个作业排入队列而最终争夺共享资源、锁等时,这非常有用。
查看 Resque 的这个插件。 我想这正是你所需要的。
如果您不介意猴子修补 resque,那么您可以使用此解决方案:
module Resque
# Monkey patch Resque to handle queues as sets instead of lists. This allows
# use to get jobs randomly rather then sequentially.
def push(queue, item)
watch_queue(queue)
redis.sadd "queue:#{queue}", encode(item)
end
def pop(queue)
decode redis.spop("queue:#{queue}")
end
def size(queue)
redis.scard("queue:#{queue}").to_i
end
end
如果正在使用 rails 使用该代码在初始化程序中创建一个文件,您将被设置。
你可以使用Delayed_job
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.