Skip to content

Commit 0776b4d

Browse files
committed
add queue code and specs
1 parent 8d6f10b commit 0776b4d

File tree

8 files changed

+420
-11
lines changed

8 files changed

+420
-11
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ source 'https://rubygems.org'
22

33
gem 'rake'
44
# Specify your gem's dependencies in resque-round-robin.gemspec
5+
gem 'linecache19', '0.5.13', :path => "~/.rvm/gems/ruby-1.9.3-p0/gems/linecache19-0.5.13/"
6+
gem 'ruby-debug-base19', '0.11.26', :path => "~/.rvm/gems/ruby-1.9.3-p0/gems/ruby-debug-base19-0.11.26/"
7+
gem 'ruby-debug19', :require => 'ruby-debug'
8+
59
gemspec
610

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ For our situation, which is probably pretty rare in rails deployments,
1111
we have multiple customers who submit jobs to resque, and we need to
1212
keep the jobs of one customer from starving out other customers.
1313

14-
Using resque-dynamic-queues in conjunction with this gem, we are able to
15-
support this requirement of fairness, where not more than one worker will
16-
process each customers job.
17-
14+
resque-dynamic-queues is a pre-requisite.
1815

1916
## Installation
2017

lib/resque-round-robin.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

22
require 'resque'
3-
require "resque/plugins/round-robin/version"
3+
require 'resque/worker'
4+
require 'resque-dynamic-queues'
5+
require "resque/plugins/round_robin/version"
6+
require "resque/plugins/round_robin/round_robin"
47

8+
Resque::Worker.send(:include, Resque::Plugins::RoundRobin)
Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11

2-
module Resque
3-
module Plugins
4-
module RoundRobin
5-
2+
module Resque::Plugins
3+
module RoundRobin
4+
def filter_busy_queues qs
5+
busy_queues = Resque::Worker.working.map { |worker| worker.job["queue"] }.compact
6+
Array(qs.dup).compact - busy_queues
67
end
7-
end
8-
end
98

9+
def rotated_queues
10+
@n ||= 0
11+
@n += 1
12+
rot_queues = queues
13+
rot_queues.rotate(@n % rot_queues.size)
14+
end
15+
16+
def reserve_with_round_robin
17+
qs = rotated_queues
18+
qs = filter_busy_queues qs
19+
qs.each do |queue|
20+
log! "Checking #{queue}"
21+
if job = Resque::Job.reserve(queue)
22+
log! "Found job on #{queue}"
23+
return job
24+
end
25+
end
26+
27+
nil
28+
rescue Exception => e
29+
log "Error reserving job: #{e.inspect}"
30+
log e.backtrace.join("\n")
31+
raise e
32+
end
33+
34+
def self.included(receiver)
35+
receiver.class_eval do
36+
alias reserve_without_round_robin reserve
37+
alias reserve reserve_with_round_robin
38+
end
39+
end
40+
41+
end # RoundRobin
42+
end # Resque::Plugins
1043

resque-round-robin.gemspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Gem::Specification.new do |gem|
99
gem.homepage = ""
1010

1111
gem.add_dependency "resque"
12+
gem.add_dependency "resque-dynamic-queues"
13+
14+
gem.add_development_dependency('rspec', '~> 2.5')
15+
gem.add_development_dependency('rack-test', '~> 0.5.4')
1216

1317
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
1418
gem.files = `git ls-files`.split("\n")

0 commit comments

Comments
 (0)