Skip to content

Commit 2bdd94d

Browse files
committed
Add RESQUE_QUEUE_DEPTH to adjust how many workers are allowed on a queue
at a time. Bump the Gem version
1 parent 6fbc62a commit 2bdd94d

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/resque/plugins/round_robin/round_robin.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,35 @@ def filter_busy_queues qs
99
def rotated_queues
1010
@n ||= 0
1111
@n += 1
12-
rot_queues = queues
12+
rot_queues = queues # since we rely on the resque-dynamic-queues plugin, this is all the queues, expanded out
1313
if rot_queues.size > 0
14-
rot_queues.rotate(@n % rot_queues.size)
14+
@n = @n % rot_queues.size
15+
rot_queues.rotate(@n)
1516
else
1617
rot_queues
1718
end
1819
end
1920

21+
def queue_depth queuename
22+
busy_queues = Resque::Worker.working.map { |worker| worker.job["queue"] }.compact
23+
# find the queuename, count it.
24+
busy_queues.select {|q| q == queuename }.size
25+
end
26+
27+
def should_work_on_queue? queuename
28+
return true if @queues.include? '*' # workers with QUEUES=* are special and are not subject to queue depth setting
29+
max = 1 # by default, workers are limited to 1 per queue
30+
max = ENV["RESQUE_QUEUE_DEPTH"].to_i if ENV["RESQUE_QUEUE_DEPTH"].present?
31+
return true if max == 0 # 0 means no limiting
32+
return true if queue_depth(queuename) < max
33+
false
34+
end
35+
2036
def reserve_with_round_robin
2137
qs = rotated_queues
22-
qs = filter_busy_queues qs
2338
qs.each do |queue|
2439
log! "Checking #{queue}"
25-
if job = Resque::Job.reserve(queue)
40+
if should_work_on_queue?(queue) && job = Resque::Job.reserve(queue)
2641
log! "Found job on #{queue}"
2742
return job
2843
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Resque
22
module Plugins
33
module RoundRobin
4-
VERSION = "0.0.2"
4+
VERSION = "0.1.0"
55
end
66
end
77
end

0 commit comments

Comments
 (0)