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

Symbolize all config.yml keys when loading

This ensures that keys in the Yaml file can be specified as either
symbols or strings, but will always be treated as symbols after loading.

It tries to use ActiveSupport's `deep_symbolize_keys!` method if
ActiveSupport is loaded, otherwise falling back to an inline version.

Closes #3672.
This commit is contained in:
Darryl Pogue 2017-12-02 12:26:25 -08:00 committed by Mike Perham
parent 4619888849
commit 280c7f7782
3 changed files with 44 additions and 1 deletions

View file

@ -214,6 +214,14 @@ module Sidekiq
@environment = cli_env || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
def symbolize_keys_deep!(hash)
hash.keys.each do |k|
symkey = k.respond_to?(:to_sym) ? k.to_sym : k
hash[symkey] = hash.delete k
symbolize_keys_deep! hash[symkey] if hash[symkey].kind_of? Hash
end
end
alias_method :die, :exit
alias_method :, :exit
@ -386,7 +394,14 @@ module Sidekiq
opts = {}
if File.exist?(cfile)
opts = YAML.load(ERB.new(IO.read(cfile)).result) || opts
opts = opts.merge(opts.delete(environment) || {})
if opts.respond_to? :deep_symbolize_keys!
opts.deep_symbolize_keys!
else
symbolize_keys_deep!(opts)
end
opts = opts.merge(opts.delete(environment.to_sym) || {})
parse_queues(opts, opts.delete(:queues) || [])
else
# allow a non-existent config file so Sidekiq

10
test/string_config.yml Normal file
View file

@ -0,0 +1,10 @@
---
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]

View file

@ -194,6 +194,24 @@ class TestCli < Sidekiq::Test
end
end
describe 'with config file using string keys' do
before do
@cli.parse(['sidekiq', '-C', './test/string_config.yml'])
end
it 'parses as expected' do
assert_equal './test/string_config.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
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 env based config file' do
before do
@cli.parse(['sidekiq', '-e', 'staging', '-C', './test/env_based_config.yml'])