From 3a15e131c9915ece61209145c68bc10310a3af79 Mon Sep 17 00:00:00 2001 From: brainopia Date: Wed, 7 Nov 2012 10:54:31 +0400 Subject: [PATCH] Support for strictly ordered queues in config --- lib/sidekiq/cli.rb | 13 ++++++++----- test/test_cli.rb | 24 ++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index 5921967f..53f471f0 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -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 diff --git a/test/test_cli.rb b/test/test_cli.rb index bf3772b5..590070a7 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -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