Skip to content

Commit

Permalink
Eager config file check
Browse files Browse the repository at this point in the history
  • Loading branch information
Tensho committed Dec 12, 2018
1 parent f013178 commit a1883bc
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions lib/sidekiq/cli.rb
Expand Up @@ -27,7 +27,7 @@ class CLI
attr_accessor :launcher
attr_accessor :environment

def parse(args=ARGV)
def parse(args = ARGV)
setup_options(args)
initialize_logger
validate!
Expand Down Expand Up @@ -227,17 +227,30 @@ def symbolize_keys_deep!(hash)
alias_method :, :exit

def setup_options(args)
# parse CLI options
opts = parse_options(args)

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
%w[config/sidekiq.yml config/sidekiq.yml.erb].each do |filename|
opts[:config_file] ||= filename if File.exist?(filename)
end
end

cfile = opts[:config_file]
opts = parse_config(cfile).merge(opts) if cfile
# parse config file options
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[:concurrency] = Integer(ENV["RAILS_MAX_THREADS"]) if !opts[:concurrency] && ENV["RAILS_MAX_THREADS"]

# merge with defaults
options.merge!(opts)
end

Expand Down Expand Up @@ -293,10 +306,6 @@ def validate!
die(1)
end

if options[:config_file] && !File.exist?(options[:config_file])
raise ArgumentError, "No such file #{options[:config_file]}"
end

[:concurrency, :timeout].each do |opt|
raise ArgumentError, "#{opt}: #{options[opt]} is not a valid value" if options.has_key?(opt) && options[opt].to_i <= 0
end
Expand Down Expand Up @@ -371,11 +380,8 @@ def parse_options(argv)
logger.info @parser
die 1
end
@parser.parse!(argv)

%w[config/sidekiq.yml config/sidekiq.yml.erb].each do |filename|
opts[:config_file] ||= filename if File.exist?(filename)
end
@parser.parse!(argv)

opts
end
Expand All @@ -396,22 +402,17 @@ def write_pid
end

def parse_config(cfile)
opts = {}
if File.exist?(cfile)
opts = YAML.load(ERB.new(IO.read(cfile)).result) || opts

if opts.respond_to? :deep_symbolize_keys!
opts.deep_symbolize_keys!
else
symbolize_keys_deep!(opts)
end
opts = YAML.load(ERB.new(IO.read(cfile)).result) || {}

opts = opts.merge(opts.delete(environment.to_sym) || {})
parse_queues(opts, opts.delete(:queues) || [])
if opts.respond_to? :deep_symbolize_keys!
opts.deep_symbolize_keys!
else
# allow a non-existent config file so Sidekiq
# can be deployed by cap with just the defaults.
symbolize_keys_deep!(opts)
end

opts = opts.merge(opts.delete(environment.to_sym) || {})
parse_queues(opts, opts.delete(:queues) || [])

ns = opts.delete(:namespace)
if ns
# logger hasn't been initialized yet, puts is all we have.
Expand Down

0 comments on commit a1883bc

Please sign in to comment.