Adding config file CLI flag

See test/config.yml for an example file.
`sidekiq -C /path/to/config.yml` will use any settings set in the file.
Flags passed to sidekiq will override any set in the config file. So, if
config.yml looked like:

---
concurrency: 50

`sidekiq -C /path/to/that/config.yml -c 75` will set the concurrency to
75.
This commit is contained in:
jc00ke 2012-02-15 18:13:32 -08:00
parent d98617b873
commit 6f22320aee
4 changed files with 157 additions and 7 deletions

View File

@ -1,3 +1,8 @@
HEAD
-----------
- Added config file support. See test/config.yml for an example file.
- Added pidfile for tools like monit
0.6.0
-----------

View File

@ -29,6 +29,7 @@ module Sidekiq
def parse(args=ARGV)
Sidekiq::Util.logger
parse_options(args)
parse_config
write_pid
validate!
boot_system
@ -64,7 +65,7 @@ module Sidekiq
def boot_system
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = detected_environment
raise ArgumentError, "#{@options[:require]} does not exist" if !File.exist?(@options[:require])
raise ArgumentError, "#{@options[:require]} does not exist" unless File.exist?(@options[:require])
if File.directory?(@options[:require])
require File.expand_path("#{@options[:require]}/config/environment.rb")
@ -100,13 +101,11 @@ module Sidekiq
@parser = OptionParser.new do |o|
o.on "-q", "--queue QUEUE,WEIGHT", "Queue to process, with optional weight" do |arg|
(q, weight) = arg.split(",")
(weight || 1).to_i.times do
@options[:queues] << q
end
parse_queues(q, weight)
end
o.on "-v", "--verbose", "Print more verbose output" do
Sidekiq::Util.logger.level = Logger::DEBUG
set_logger_level_to_debug
end
o.on "-n", "--namespace NAMESPACE", "namespace worker queues are under" do |arg|
@ -129,9 +128,13 @@ module Sidekiq
@options[:processor_count] = arg.to_i
end
o.on '-P', '--pidfile PATH', "path to use" do |arg|
o.on '-P', '--pidfile PATH', "path to pidfile" do |arg|
@options[:pidfile] = arg
end
o.on '-C', '--config PATH', "path to YAML config file" do |arg|
@options[:config_file] = arg
end
end
@parser.banner = "sidekiq [options]"
@ -150,5 +153,39 @@ module Sidekiq
end
end
def parse_config
if @options[:config_file] && File.exist?(@options[:config_file])
require 'yaml'
opts = YAML.load_file @options[:config_file]
queues = if @options[:queues].empty?
opts.delete('queues')
else
[]
end
queues.each { |pair| parse_queues(*pair) }
require_file = opts.delete('require')
if @options[:require] == '.' && require_file
@options[:require] = require_file
end
opts.each do |option, value|
@options[option.intern] ||= value
end
@options[:concurrency] = opts['processor_count'] if opts['processor_count']
set_logger_level_to_debug if @options[:verbose]
end
end
def parse_queues(q, weight)
(weight || 1).to_i.times do
@options[:queues] << q
end
end
def set_logger_level_to_debug
Sidekiq::Util.logger.level = Logger::DEBUG
end
end
end

11
test/config.yml Normal file
View File

@ -0,0 +1,11 @@
---
verbose: false
namespace: test_namespace
server: 127.0.0.1:1234
environment: xzibit
require: ./test/fake_env.rb
pidfile: /tmp/sidekiq-config-test.pid
processor_count: 50
queues:
- [often, 2]
- [seldom, 1]

View File

@ -40,7 +40,7 @@ class TestCli < MiniTest::Unit::TestCase
@tmp_file = Tempfile.new('sidekiq-test')
@tmp_path = @tmp_file.path
@tmp_file.close!
File.unlink @tmp_path if File.exist? @tmp_path
@cli.parse(['sidekiq', '-P', @tmp_path, '-r', './test/fake_env.rb'])
end
@ -57,6 +57,103 @@ class TestCli < MiniTest::Unit::TestCase
end
end
describe 'with config file' do
before do
@cli.parse(['sidekiq', '-C', './test/config.yml'])
end
it 'takes a path' do
assert_equal './test/config.yml', @cli.options[:config_file]
end
it 'sets verbose' do
refute @cli.options[:verbose]
end
it 'sets namespace' do
assert_equal "test_namespace", @cli.options[:namespace]
end
it 'sets require file' do
assert_equal './test/fake_env.rb', @cli.options[:require]
end
it 'sets environment' do
assert_equal 'xzibit', @cli.options[:environment]
end
it 'sets concurrency' do
assert_equal 50, @cli.options[:concurrency]
end
it 'sets pid file' do
assert_equal '/tmp/sidekiq-config-test.pid', @cli.options[:pidfile]
end
it 'sets queues' do
assert_equal 2, @cli.options[:queues].select{ |q| q == 'often' }.length
assert_equal 1, @cli.options[:queues].select{ |q| q == 'seldom' }.length
end
end
describe 'with config file and flags' do
before do
# We need an actual file here.
@tmp_lib_path = '/tmp/require-me.rb'
File.open(@tmp_lib_path, 'w') do |f|
f.puts "# do work"
end
@tmp_file = Tempfile.new('sidekiqr')
@tmp_path = @tmp_file.path
@tmp_file.close!
@cli.parse(['sidekiq',
'-C', './test/config.yml',
'-n', 'sweet_story_bro',
'-e', 'snoop',
'-c', '100',
'-r', @tmp_lib_path,
'-P', @tmp_path,
'-q', 'often,7',
'-q', 'seldom,3'])
end
after do
File.unlink @tmp_lib_path if File.exist? @tmp_lib_path
File.unlink @tmp_path if File.exist? @tmp_path
end
it 'uses processor count flag' do
assert_equal 100, @cli.options[:processor_count]
end
it 'uses namespace flag' do
assert_equal "sweet_story_bro", @cli.options[:namespace]
end
it 'uses require file flag' do
assert_equal @tmp_lib_path, @cli.options[:require]
end
it 'uses environment flag' do
assert_equal 'snoop', @cli.options[:environment]
end
it 'uses concurrency flag' do
assert_equal 100, @cli.options[:processor_count]
end
it 'uses pidfile flag' do
assert_equal @tmp_path, @cli.options[:pidfile]
end
it 'sets queues' do
assert_equal 7, @cli.options[:queues].select{ |q| q == 'often' }.length
assert_equal 3, @cli.options[:queues].select{ |q| q == 'seldom' }.length
end
end
def new_cli
cli = Sidekiq::CLI.new
def cli.die(code)