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

Merge pull request #630 from dtaniwaki/env_based_config

Support env based config file
This commit is contained in:
Mike Perham 2013-01-18 20:21:29 -08:00
commit 9da37227b9
3 changed files with 53 additions and 5 deletions

View file

@ -59,12 +59,14 @@ module Sidekiq
@code = nil
@interrupt_mutex = Mutex.new
@interrupted = false
@environment = ENV['RAILS_ENV'] || ENV['RACK_ENV']
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 || 'development'
raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require])
@ -246,6 +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.delete(@environment)) if @environment && opts[@environment].is_a?(Hash)
parse_queues opts, opts.delete(:queues) || []
end
opts

10
test/env_based_config.yml Normal file
View file

@ -0,0 +1,10 @@
---
:pidfile: /tmp/sidekiq-config-test.pid
staging:
:verbose: false
:require: ./test/fake_env.rb
:logfile: /tmp/sidekiq.log
:concurrency: 50
:queues:
- [<%="very_"%>often, 2]
- [seldom, 1]

View file

@ -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.