mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Check Config File Existence (#4054)
* Check config file existence * Eager config file check * Parse expanded path to default sidekiq.yml config file in Rails app * Cleanup
This commit is contained in:
parent
6a7bf2d0de
commit
1d835551f0
3 changed files with 56 additions and 27 deletions
|
@ -27,7 +27,7 @@ module Sidekiq
|
||||||
attr_accessor :launcher
|
attr_accessor :launcher
|
||||||
attr_accessor :environment
|
attr_accessor :environment
|
||||||
|
|
||||||
def parse(args=ARGV)
|
def parse(args = ARGV)
|
||||||
setup_options(args)
|
setup_options(args)
|
||||||
initialize_logger
|
initialize_logger
|
||||||
validate!
|
validate!
|
||||||
|
@ -227,17 +227,33 @@ module Sidekiq
|
||||||
alias_method :☠, :exit
|
alias_method :☠, :exit
|
||||||
|
|
||||||
def setup_options(args)
|
def setup_options(args)
|
||||||
|
# parse CLI options
|
||||||
opts = parse_options(args)
|
opts = parse_options(args)
|
||||||
|
|
||||||
set_environment opts[:environment]
|
set_environment opts[:environment]
|
||||||
|
|
||||||
options[:queues] << 'default' if options[:queues].empty?
|
# check config file presence
|
||||||
|
if opts[:config_file]
|
||||||
|
if opts[:config_file] && !File.exist?(opts[:config_file])
|
||||||
|
raise ArgumentError, "No such file #{opts[:config_file]}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if File.directory?(opts[:require])
|
||||||
|
%w[config/sidekiq.yml config/sidekiq.yml.erb].each do |filename|
|
||||||
|
path = File.expand_path(filename, opts[:require])
|
||||||
|
opts[:config_file] ||= path if File.exist?(path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
cfile = opts[:config_file]
|
# parse config file options
|
||||||
opts = parse_config(cfile).merge(opts) if cfile
|
opts = parse_config(opts[:config_file]).merge(opts) if opts[:config_file]
|
||||||
|
|
||||||
|
opts[:queues] = Array(opts[:queues]) << 'default' if opts[:queues].nil? || opts[:queues].empty?
|
||||||
opts[:strict] = true if opts[:strict].nil?
|
opts[:strict] = true if opts[:strict].nil?
|
||||||
opts[:concurrency] = Integer(ENV["RAILS_MAX_THREADS"]) if !opts[:concurrency] && ENV["RAILS_MAX_THREADS"]
|
opts[:concurrency] = Integer(ENV["RAILS_MAX_THREADS"]) if opts[:concurrency].nil? && ENV["RAILS_MAX_THREADS"]
|
||||||
|
|
||||||
|
# merge with defaults
|
||||||
options.merge!(opts)
|
options.merge!(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -367,11 +383,8 @@ module Sidekiq
|
||||||
logger.info @parser
|
logger.info @parser
|
||||||
die 1
|
die 1
|
||||||
end
|
end
|
||||||
@parser.parse!(argv)
|
|
||||||
|
|
||||||
%w[config/sidekiq.yml config/sidekiq.yml.erb].each do |filename|
|
@parser.parse!(argv)
|
||||||
opts[:config_file] ||= filename if File.exist?(filename)
|
|
||||||
end
|
|
||||||
|
|
||||||
opts
|
opts
|
||||||
end
|
end
|
||||||
|
@ -391,10 +404,8 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_config(cfile)
|
def parse_config(path)
|
||||||
opts = {}
|
opts = YAML.load(ERB.new(File.read(path)).result) || {}
|
||||||
if File.exist?(cfile)
|
|
||||||
opts = YAML.load(ERB.new(IO.read(cfile)).result) || opts
|
|
||||||
|
|
||||||
if opts.respond_to? :deep_symbolize_keys!
|
if opts.respond_to? :deep_symbolize_keys!
|
||||||
opts.deep_symbolize_keys!
|
opts.deep_symbolize_keys!
|
||||||
|
@ -404,10 +415,7 @@ module Sidekiq
|
||||||
|
|
||||||
opts = opts.merge(opts.delete(environment.to_sym) || {})
|
opts = opts.merge(opts.delete(environment.to_sym) || {})
|
||||||
parse_queues(opts, opts.delete(:queues) || [])
|
parse_queues(opts, opts.delete(:queues) || [])
|
||||||
else
|
|
||||||
# allow a non-existent config file so Sidekiq
|
|
||||||
# can be deployed by cap with just the defaults.
|
|
||||||
end
|
|
||||||
ns = opts.delete(:namespace)
|
ns = opts.delete(:namespace)
|
||||||
if ns
|
if ns
|
||||||
# logger hasn't been initialized yet, puts is all we have.
|
# logger hasn't been initialized yet, puts is all we have.
|
||||||
|
@ -421,10 +429,10 @@ module Sidekiq
|
||||||
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
|
||||||
|
|
||||||
def parse_queue(opts, q, weight=nil)
|
def parse_queue(opts, queue, weight = nil)
|
||||||
opts[:queues] ||= []
|
opts[:queues] ||= []
|
||||||
raise ArgumentError, "queues: #{q} cannot be defined twice" if opts[:queues].include?(q)
|
raise ArgumentError, "queues: #{queue} cannot be defined twice" if opts[:queues].include?(queue)
|
||||||
[weight.to_i, 1].max.times { opts[:queues] << q }
|
[weight.to_i, 1].max.times { opts[:queues] << queue }
|
||||||
opts[:strict] = false if weight.to_i > 0
|
opts[:strict] = false if weight.to_i > 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
2
test/dummy/config/sidekiq.yml
Normal file
2
test/dummy/config/sidekiq.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
---
|
||||||
|
:concurrency: 25
|
|
@ -244,6 +244,17 @@ class TestCLI < Minitest::Test
|
||||||
assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
|
assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'default config file' do
|
||||||
|
describe 'when required path is a directory' do
|
||||||
|
it 'tries config/sidekiq.yml' do
|
||||||
|
@cli.parse(%w[sidekiq -r ./test/dummy])
|
||||||
|
|
||||||
|
assert_equal 'sidekiq.yml', File.basename(Sidekiq.options[:config_file])
|
||||||
|
assert_equal 25, Sidekiq.options[:concurrency]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -260,6 +271,14 @@ class TestCLI < Minitest::Test
|
||||||
exit = assert_raises(SystemExit) { @cli.parse(%w[sidekiq -r ./test/fixtures]) }
|
exit = assert_raises(SystemExit) { @cli.parse(%w[sidekiq -r ./test/fixtures]) }
|
||||||
assert_equal 1, exit.status
|
assert_equal 1, exit.status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'when config file path does not exist' do
|
||||||
|
it 'raises argument error' do
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
@cli.parse(%w[sidekiq -r ./test/fake_env.rb -C /non/existent/path])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue