1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Support for strictly ordered queues in config

This commit is contained in:
brainopia 2012-11-07 10:54:31 +04:00
parent f3c6c65eb0
commit 3a15e131c9
2 changed files with 30 additions and 7 deletions

View file

@ -152,8 +152,7 @@ module Sidekiq
@parser = OptionParser.new do |o|
o.on "-q", "--queue QUEUE[,WEIGHT]...", "Queues to process with optional weights" do |arg|
queues_and_weights = arg.scan(/([\w\.-]+),?(\d*)/)
queues_and_weights.each {|queue_and_weight| parse_queues(opts, *queue_and_weight)}
opts[:strict] = queues_and_weights.collect(&:last).none? {|weight| weight != ''}
parse_queues opts, queues_and_weights
end
o.on "-v", "--verbose", "Print more verbose output" do
@ -215,13 +214,17 @@ module Sidekiq
opts = {}
if cli[:config_file] && File.exist?(cli[:config_file])
opts = YAML.load(ERB.new(IO.read(cli[:config_file])).result)
queues = opts.delete(:queues) || []
queues.each { |name, weight| parse_queues(opts, name, weight) }
parse_queues opts, opts.delete(:queues) || []
end
opts
end
def parse_queues(opts, q, weight)
def parse_queues(opts, queues_and_weights)
queues_and_weights.each {|queue_and_weight| parse_queue(opts, *queue_and_weight)}
opts[:strict] = queues_and_weights.all? {|_, weight| weight.to_s.empty? }
end
def parse_queue(opts, q, weight=nil)
[weight.to_i, 1].max.times do
(opts[:queues] ||= []) << q
end

View file

@ -190,10 +190,30 @@ class TestCli < MiniTest::Unit::TestCase
end
describe 'Sidekiq::CLI#parse_queues' do
describe 'when weight is present' do
it 'concatenates queues by factor of weight and sets strict to false' do
opts = {}
@cli.send :parse_queues, opts, [['often', 7]]
assert_equal %w[often] * 7, opts[:queues]
assert !opts[:strict]
end
end
describe 'when weight is not present' do
it 'returns queues and sets strict' do
opts = {}
@cli.send :parse_queues, opts, [['once']]
assert_equal %w[once], opts[:queues]
assert opts[:strict]
end
end
end
describe 'Sidekiq::CLI#parse_queue' do
describe 'when weight is present' do
it 'concatenates queue to opts[:queues] weight number of times' do
opts = {}
@cli.send :parse_queues, opts, 'often', 7
@cli.send :parse_queue, opts, 'often', 7
assert_equal %w[often] * 7, opts[:queues]
end
end
@ -201,7 +221,7 @@ class TestCli < MiniTest::Unit::TestCase
describe 'when weight is not present' do
it 'concatenates queue to opts[:queues] once' do
opts = {}
@cli.send :parse_queues, opts, 'once', nil
@cli.send :parse_queue, opts, 'once', nil
assert_equal %w[once], opts[:queues]
end
end