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

Load aliases for Psych 4 (included in Ruby 3.1) (#5141)

This commit is contained in:
Peter Goldstein 2022-01-24 13:32:36 -08:00 committed by GitHub
parent 24fb5d3e50
commit 860c7b1909
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 1 deletions

View file

@ -382,7 +382,7 @@ module Sidekiq
def parse_config(path) def parse_config(path)
erb = ERB.new(File.read(path)) erb = ERB.new(File.read(path))
erb.filename = File.expand_path(path) erb.filename = File.expand_path(path)
opts = YAML.load(erb.result) || {} opts = load_yaml(erb.result) || {}
if opts.respond_to? :deep_symbolize_keys! if opts.respond_to? :deep_symbolize_keys!
opts.deep_symbolize_keys! opts.deep_symbolize_keys!
@ -398,6 +398,14 @@ module Sidekiq
opts opts
end end
def load_yaml(src)
if Psych::VERSION > "4.0"
YAML.safe_load(src, permitted_classes: [Symbol], aliases: true)
else
YAML.load(src)
end
end
def parse_queues(opts, queues_and_weights) def parse_queues(opts, queues_and_weights)
queues_and_weights.each { |queue_and_weight| parse_queue(opts, *queue_and_weight) } queues_and_weights.each { |queue_and_weight| parse_queue(opts, *queue_and_weight) }
end end

View file

@ -0,0 +1,12 @@
---
:production: &production
:verbose: true
:require: ./test/fake_env.rb
:concurrency: 50
:queues:
- [<%="very_"%>often, 2]
- [seldom, 1]
:staging:
<<: *production
:verbose: false

View file

@ -198,6 +198,26 @@ describe Sidekiq::CLI do
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' } assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end end
it 'accepts environment specific config with alias' do
subject.parse(%w[sidekiq -e staging -C ./test/config_with_alias.yml])
assert_equal './test/config_with_alias.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'staging', Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
subject.parse(%w[sidekiq -e production -C ./test/config_with_alias.yml])
assert_equal './test/config_with_alias.yml', Sidekiq.options[:config_file]
assert Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'production', Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
it 'exposes ERB expected __FILE__ and __dir__' do it 'exposes ERB expected __FILE__ and __dir__' do
given_path = './test/config__FILE__and__dir__.yml' given_path = './test/config__FILE__and__dir__.yml'
expected_file = File.expand_path(given_path) expected_file = File.expand_path(given_path)