Skip to content

Commit 1d89dc6

Browse files
committed
Merge pull request resque#575 from tenderlove/resque
--- MultiQueue#poll will allow us to use a blocking pop in our runloop, but timeout and check for process shutdown.
2 parents 5928531 + 4619599 commit 1d89dc6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/resque/multi_queue.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,20 @@ def pop(non_block = false)
5454
end
5555
end
5656
end
57+
58+
# Retrieves data from the queue head, and removes it.
59+
#
60+
# Blocks for +timeout+ seconds if the queue is empty, and returns nil if
61+
# the timeout expires.
62+
def poll(timeout)
63+
queue_names = @queues.map {|queue| queue.redis_name }
64+
queue_name, payload = @redis.blpop(*(queue_names + [timeout]))
65+
return unless payload
66+
67+
synchronize do
68+
queue = @queue_hash[queue_name]
69+
[queue, queue.decode(payload)]
70+
end
71+
end
5772
end
5873
end

test/multi_queue_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
redis.flushall
99
end
1010

11+
it "poll times out and returns nil" do
12+
foo = Resque::Queue.new 'foo', redis
13+
bar = Resque::Queue.new 'bar', redis
14+
queue = Resque::MultiQueue.new([foo, bar], redis)
15+
assert_nil queue.poll(1)
16+
end
17+
1118
it "blocks on pop" do
1219
foo = Resque::Queue.new 'foo', redis, coder
1320
bar = Resque::Queue.new 'bar', redis, coder

0 commit comments

Comments
 (0)