From 86f069cff7a99fec77e18a9720b0d316872ddec2 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Fri, 18 Jan 2013 12:51:16 -0500 Subject: [PATCH 1/3] Support env based config file --- lib/sidekiq/cli.rb | 3 +++ test/env_based_config.yml | 10 ++++++++++ test/test_cli.rb | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 test/env_based_config.yml diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index 8f4e77be..d9e64a2e 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -246,6 +246,9 @@ module Sidekiq opts = {} if cli[:config_file] && File.exist?(cli[:config_file]) opts = YAML.load(ERB.new(IO.read(cli[:config_file])).result) + if env = cli[:environment] + opts = opts[env] || opts + end parse_queues opts, opts.delete(:queues) || [] end opts diff --git a/test/env_based_config.yml b/test/env_based_config.yml new file mode 100644 index 00000000..447bbf86 --- /dev/null +++ b/test/env_based_config.yml @@ -0,0 +1,10 @@ +--- +staging: + :verbose: false + :require: ./test/fake_env.rb + :pidfile: /tmp/sidekiq-config-test.pid + :logfile: /tmp/sidekiq.log + :concurrency: 50 + :queues: + - [<%="very_"%>often, 2] + - [seldom, 1] diff --git a/test/test_cli.rb b/test/test_cli.rb index 1738be03..63f2fe0b 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -191,6 +191,45 @@ class TestCli < MiniTest::Unit::TestCase end end + describe 'with env based config file' do + before do + @cli.parse(['sidekiq', '-e', 'staging', '-C', './test/env_based_config.yml']) + end + + it 'takes a path' do + assert_equal './test/env_based_config.yml', Sidekiq.options[:config_file] + end + + it 'sets verbose' do + refute Sidekiq.options[:verbose] + end + + it 'sets require file' do + assert_equal './test/fake_env.rb', Sidekiq.options[:require] + end + + it 'sets environment' do + assert_equal 'staging', Sidekiq.options[:environment] + end + + it 'sets concurrency' do + assert_equal 50, Sidekiq.options[:concurrency] + end + + it 'sets pid file' do + assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile] + end + + it 'sets logfile' do + assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile] + end + + it 'sets queues' do + assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' } + assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' } + end + end + describe 'with config file and flags' do before do # We need an actual file here. From 2d6672b7d78d8b13cc6a6e2077a51454a3beb1d1 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Fri, 18 Jan 2013 15:41:55 -0500 Subject: [PATCH 2/3] Merge env based config to top level config and use not only command line options but also RAILS_ENV and RACK_ENV. --- lib/sidekiq/cli.rb | 12 ++++-------- test/env_based_config.yml | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index d9e64a2e..fd4f75cb 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -59,12 +59,14 @@ module Sidekiq @code = nil @interrupt_mutex = Mutex.new @interrupted = false + @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' end def parse(args=ARGV) @code = nil cli = parse_options(args) + @environment = cli[:environment] if cli[:environment] config = parse_config(cli) options.merge!(config.merge(cli)) @@ -118,12 +120,8 @@ module Sidekiq Sidekiq.options end - def detected_environment - options[:environment] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' - end - def boot_system - ENV['RACK_ENV'] = ENV['RAILS_ENV'] = detected_environment + ENV['RACK_ENV'] = ENV['RAILS_ENV'] = @environment raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require]) @@ -246,9 +244,7 @@ module Sidekiq opts = {} if cli[:config_file] && File.exist?(cli[:config_file]) opts = YAML.load(ERB.new(IO.read(cli[:config_file])).result) - if env = cli[:environment] - opts = opts[env] || opts - end + opts = opts.merge(opts[@environment]) if opts[@environment].is_a?(Hash) parse_queues opts, opts.delete(:queues) || [] end opts diff --git a/test/env_based_config.yml b/test/env_based_config.yml index 447bbf86..4a93bfe9 100644 --- a/test/env_based_config.yml +++ b/test/env_based_config.yml @@ -1,8 +1,8 @@ --- +:pidfile: /tmp/sidekiq-config-test.pid staging: :verbose: false :require: ./test/fake_env.rb - :pidfile: /tmp/sidekiq-config-test.pid :logfile: /tmp/sidekiq.log :concurrency: 50 :queues: From 06e82239d0e27a41031ab761b8c8c225c38a1e4d Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Fri, 18 Jan 2013 15:52:56 -0500 Subject: [PATCH 3/3] Refactering for organized log --- lib/sidekiq/cli.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index fd4f75cb..e735a396 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -59,7 +59,7 @@ module Sidekiq @code = nil @interrupt_mutex = Mutex.new @interrupted = false - @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' + @environment = ENV['RAILS_ENV'] || ENV['RACK_ENV'] end def parse(args=ARGV) @@ -121,7 +121,7 @@ module Sidekiq end def boot_system - ENV['RACK_ENV'] = ENV['RAILS_ENV'] = @environment + ENV['RACK_ENV'] = ENV['RAILS_ENV'] = @environment || 'development' raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require]) @@ -244,7 +244,7 @@ module Sidekiq opts = {} if cli[:config_file] && File.exist?(cli[:config_file]) opts = YAML.load(ERB.new(IO.read(cli[:config_file])).result) - opts = opts.merge(opts[@environment]) if opts[@environment].is_a?(Hash) + opts = opts.merge(opts.delete(@environment)) if @environment && opts[@environment].is_a?(Hash) parse_queues opts, opts.delete(:queues) || [] end opts