【发布时间】:2013-07-09 16:36:20
【问题描述】:
我正在尝试理解 Resque 的排序逻辑。我相信每个队列都是在 FIFO 基础上处理的(标准队列行为),但是说我有几个队列 - 都有待处理的作业 - 以及两个处理“*”的工作人员(即所有队列)。
什么算法决定接下来要处理哪个作业?如果它是一个通用 FIFO,它将是 any 队列中最旧的作业,但在我看来,它就像是在进行某种队列轮换。
【问题讨论】:
标签: ruby scheduled-tasks resque
我正在尝试理解 Resque 的排序逻辑。我相信每个队列都是在 FIFO 基础上处理的(标准队列行为),但是说我有几个队列 - 都有待处理的作业 - 以及两个处理“*”的工作人员(即所有队列)。
什么算法决定接下来要处理哪个作业?如果它是一个通用 FIFO,它将是 any 队列中最旧的作业,但在我看来,它就像是在进行某种队列轮换。
【问题讨论】:
标签: ruby scheduled-tasks resque
见https://github.com/resque/resque/blob/master/lib/resque/multi_queue.rb
如果它在阻塞模式下工作(默认情况下),那么它使用 Redis 的 BLPOP 方法,该方法接受队列列表,并从第一个队列中弹出并返回一个值以获取数据。
Redis 的 BLPOP 以先到先得的方式将客户端排入队列。当给定多个队列键时,它只是遍历它们并为每个键设置阻塞。见https://github.com/antirez/redis/blob/unstable/src/t_list.c#L781-815
Resque 将通过获取 SMEMBERS queues 之类的内容来构建要测试的队列列表,这意味着队列将按照 SMEMBERS 命令返回它们的顺序排列优先级。这是一个设置操作,这意味着它的订单未定义;你主要受 Redis 的摆布。
【讨论】:
也许你应该知道一点redis,因为resque内部用户[blpop][1]检查here
现在blpop 是如何工作的,假设你有 2 个列表或队列 @987654325@ 和 queen
考虑以下场景
1) when one or more message exist in `king` and `queen` has no message i.e empty
blop would pop a single message from `king`
2) when `queen` has one or more message but `king` doesnt
blop would pop a message from `queen`
3) when but `king` or `queen` both has message in them.
now the order of queues decide from which the message would be popped
Let say `king` has 3 message and `queen` has 2 message
and let say the order is
redis.blpop "king","queen",0
blop would pop message from `king` 3 times(until it has no longer message in them) and then would pop the message from queue name "queen"
您可以参考 redis documentation 在列表和 blop 上了解更多信息
希望这是有道理的
【讨论】:
blpop?他们的自述文件特别说默认是每 5 秒轮询一次,redis monitor 日志显示 lpop 正在使用。这是最近的行为,是否可以切换到blpop 模式?