Skip to content

Commit

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

MultiQueue#poll will allow us to use a blocking pop in our runloop, but timeout and check for process shutdown.
  • Loading branch information
hone committed Apr 25, 2012
2 parents 5928531 + 4619599 commit 1d89dc6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/resque/multi_queue.rb
Expand Up @@ -54,5 +54,20 @@ def pop(non_block = false)
end
end
end

# Retrieves data from the queue head, and removes it.
#
# Blocks for +timeout+ seconds if the queue is empty, and returns nil if
# the timeout expires.
def poll(timeout)
queue_names = @queues.map {|queue| queue.redis_name }
queue_name, payload = @redis.blpop(*(queue_names + [timeout]))
return unless payload

synchronize do
queue = @queue_hash[queue_name]
[queue, queue.decode(payload)]
end
end
end
end
7 changes: 7 additions & 0 deletions test/multi_queue_test.rb
Expand Up @@ -8,6 +8,13 @@
redis.flushall
end

it "poll times out and returns nil" do
foo = Resque::Queue.new 'foo', redis
bar = Resque::Queue.new 'bar', redis
queue = Resque::MultiQueue.new([foo, bar], redis)
assert_nil queue.poll(1)
end

it "blocks on pop" do
foo = Resque::Queue.new 'foo', redis, coder
bar = Resque::Queue.new 'bar', redis, coder
Expand Down

0 comments on commit 1d89dc6

Please sign in to comment.