diff --git a/lib/sidekiq/cli.rb b/lib/sidekiq/cli.rb index fabb4c0b..85593294 100644 --- a/lib/sidekiq/cli.rb +++ b/lib/sidekiq/cli.rb @@ -29,6 +29,7 @@ module Sidekiq def parse(args=ARGV) Sidekiq::Util.logger parse_options(args) + write_pid validate! boot_system end @@ -127,6 +128,10 @@ module Sidekiq o.on '-c', '--concurrency INT', "processor threads to use" do |arg| @options[:processor_count] = arg.to_i end + + o.on '-p', '--pidfile PATH', "path to use" do |arg| + @options[:pidfile] = arg + end end @parser.banner = "sidekiq [options]" @@ -137,5 +142,13 @@ module Sidekiq @parser.parse!(argv) end + def write_pid + if path = @options[:pidfile] + File.open(path, 'w') do |f| + f.puts Process.pid + end + end + end + end end diff --git a/test/test_cli.rb b/test/test_cli.rb index 2b91d905..9079c477 100644 --- a/test/test_cli.rb +++ b/test/test_cli.rb @@ -1,5 +1,6 @@ require 'helper' require 'sidekiq/cli' +require 'tempfile' class TestCli < MiniTest::Unit::TestCase describe 'with cli' do @@ -34,6 +35,28 @@ class TestCli < MiniTest::Unit::TestCase assert_equal %w(bar foo foo foo), @cli.options[:queues].sort end + describe 'with pidfile' do + before do + @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 + + after do + File.unlink @tmp_path if File.exist? @tmp_path + end + + it 'sets pidfile path' do + assert_equal @tmp_path, @cli.options[:pidfile] + end + + it 'writes pidfile' do + assert_equal File.read(@tmp_path).strip.to_i, Process.pid + end + end + def new_cli cli = Sidekiq::CLI.new def cli.die(code)