Skip to content

Commit

Permalink
Merge pull request #565 from tenderlove/resque
Browse files Browse the repository at this point in the history
---

blpop can take a 0 timeout, so we can avoid looping with exceptions in the non blocking case.
  • Loading branch information
hone committed Apr 13, 2012
2 parents d393148 + 2fcf6da commit 02156b1
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/resque/multi_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(queues, redis)

@queues = {}
@redis = redis
@q_list = queues

queues.each do |queue|
key = @redis.is_a?(Redis::Namespace) ? "#{@redis.namespace}:" : ""
Expand All @@ -32,25 +33,25 @@ def initialize(queues, redis)
def pop(non_block = false)
if non_block
synchronize do
value = nil
queue_name, payload = @redis.blpop(*(queue_names + [0]))

@queues.values.each do |queue|
begin
return queue.pop(true)
rescue ThreadError
end
end

raise ThreadError
raise ThreadError unless queue_name && payload
@queues[queue_name].decode(payload)
end
else
queue_names = @queues.values.map {|queue| queue.redis_name }
synchronize do
value = @redis.blpop(*(queue_names + [1])) until value
queue_name, payload = value
@queues[queue_name].decode(payload)
end
end
end

private
def queue_names
# possibly refactor this to set an ivar of the list in the constructor.
# We don't need to calculate the list on every call to `pop`.
@q_list.map {|queue| queue.redis_name }
end
end
end

0 comments on commit 02156b1

Please sign in to comment.