mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Clean up option processing
This commit is contained in:
parent
9da37227b9
commit
4cca6eb85d
4 changed files with 45 additions and 16 deletions
16
Changes.md
16
Changes.md
|
@ -1,3 +1,19 @@
|
|||
HEAD
|
||||
-----------
|
||||
|
||||
- Allow environment-specific sections within the config file which
|
||||
override the global values [dtaniwaki, #630]
|
||||
|
||||
```
|
||||
---
|
||||
:concurrency: 50
|
||||
:verbose: false
|
||||
staging:
|
||||
:verbose: true
|
||||
:concurrency: 5
|
||||
```
|
||||
|
||||
|
||||
2.6.5
|
||||
-----------
|
||||
|
||||
|
|
|
@ -54,22 +54,18 @@ module Sidekiq
|
|||
# Used for CLI testing
|
||||
attr_accessor :code
|
||||
attr_accessor :manager
|
||||
attr_accessor :environment
|
||||
|
||||
def initialize
|
||||
@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))
|
||||
|
||||
setup_options(args)
|
||||
initialize_logger
|
||||
validate!
|
||||
write_pid
|
||||
|
@ -112,16 +108,30 @@ module Sidekiq
|
|||
|
||||
private
|
||||
|
||||
def set_environment(cli_env)
|
||||
@environment = cli_env || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
||||
end
|
||||
|
||||
def die(code)
|
||||
exit(code)
|
||||
end
|
||||
|
||||
def setup_options(args)
|
||||
cli = parse_options(args)
|
||||
set_environment cli[:environment]
|
||||
|
||||
cfile = cli[:config_file]
|
||||
|
||||
config = (cfile ? parse_config(cfile) : {})
|
||||
options.merge!(config.merge(cli))
|
||||
end
|
||||
|
||||
def options
|
||||
Sidekiq.options
|
||||
end
|
||||
|
||||
def boot_system
|
||||
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = @environment || 'development'
|
||||
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment
|
||||
|
||||
raise ArgumentError, "#{options[:require]} does not exist" unless File.exist?(options[:require])
|
||||
|
||||
|
@ -240,12 +250,14 @@ module Sidekiq
|
|||
end
|
||||
end
|
||||
|
||||
def parse_config(cli)
|
||||
def parse_config(cfile)
|
||||
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)
|
||||
if File.exist?(cfile)
|
||||
opts = YAML.load(ERB.new(IO.read(cfile)).result)
|
||||
opts = opts.merge(opts.delete(environment) || {})
|
||||
parse_queues opts, opts.delete(:queues) || []
|
||||
else
|
||||
raise ArgumentError, "can't find config file #{cfile}"
|
||||
end
|
||||
opts
|
||||
end
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
---
|
||||
:pidfile: /tmp/sidekiq-config-test.pid
|
||||
:concurrency: 50
|
||||
staging:
|
||||
:verbose: false
|
||||
:require: ./test/fake_env.rb
|
||||
:logfile: /tmp/sidekiq.log
|
||||
:concurrency: 50
|
||||
:concurrency: 5
|
||||
:queues:
|
||||
- [<%="very_"%>often, 2]
|
||||
- [seldom, 1]
|
||||
|
|
|
@ -112,7 +112,7 @@ class TestCli < MiniTest::Unit::TestCase
|
|||
|
||||
Sidekiq.logger.info('test message')
|
||||
|
||||
assert_match /test message/, File.read(@tmp_log_path), "didn't include the log message"
|
||||
assert_match(/test message/, File.read(@tmp_log_path), "didn't include the log message")
|
||||
end
|
||||
|
||||
it 'appends messages to a logfile' do
|
||||
|
@ -125,8 +125,8 @@ class TestCli < MiniTest::Unit::TestCase
|
|||
Sidekiq.logger.info('test message')
|
||||
|
||||
log_file_content = File.read(@tmp_log_path)
|
||||
assert_match /already existant/, log_file_content, "didn't include the old message"
|
||||
assert_match /test message/, log_file_content, "didn't include the new message"
|
||||
assert_match(/already existant/, log_file_content, "didn't include the old message")
|
||||
assert_match(/test message/, log_file_content, "didn't include the new message")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -213,7 +213,7 @@ class TestCli < MiniTest::Unit::TestCase
|
|||
end
|
||||
|
||||
it 'sets concurrency' do
|
||||
assert_equal 50, Sidekiq.options[:concurrency]
|
||||
assert_equal 5, Sidekiq.options[:concurrency]
|
||||
end
|
||||
|
||||
it 'sets pid file' do
|
||||
|
|
Loading…
Add table
Reference in a new issue