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

92 lines
1.8 KiB
Text
Raw Normal View History

#!/usr/bin/env ruby
require 'fileutils'
class Sidekiqctl
2014-03-13 13:17:11 -04:00
DEFAULT_TIMEOUT = 10
attr_reader :stage, :pidfile, :timeout
2014-03-13 13:17:11 -04:00
def self.print_usage
puts
puts "Usage: #{File.basename($0)} <command> <pidfile> <timeout>"
puts " where <command> is either 'quiet', 'stop' or 'shutdown'"
puts " <pidfile> is path to a pidfile"
puts " <timeout> is number of seconds to wait till Sidekiq exits (default: #{Sidekiqctl::DEFAULT_TIMEOUT})"
puts
end
def initialize(stage, pidfile, timeout)
@stage = stage
@pidfile = pidfile
@timeout = timeout
done('No pidfile given', :error) if !pidfile
done("Pidfile #{pidfile} does not exist", :warn) if !File.exist?(pidfile)
done('Invalid pidfile content', :error) if pid == 0
fetch_process
begin
send(stage)
rescue NoMethodError
done 'Invalid control command', :error
end
end
def fetch_process
Process.getpgid(pid)
rescue Errno::ESRCH
done "Process doesn't exist", :error
end
def done(msg, error = nil)
puts msg
exit(exit_signal(error))
end
def exit_signal(error)
(error == :error) ? 1 : 0
end
def pid
@pid ||= File.read(pidfile).to_i
end
def quiet
`kill -USR1 #{pid}`
end
def stop
`kill -TERM #{pid}`
timeout.times do
begin
Process.getpgid(pid)
rescue Errno::ESRCH
FileUtils.rm_f pidfile
done 'Sidekiq shut down gracefully.'
end
sleep 1
end
`kill -9 #{pid}`
2012-03-28 21:51:57 -04:00
FileUtils.rm_f pidfile
done 'Sidekiq shut down forcefully.'
end
def shutdown
quiet
stop
end
end
2014-03-13 13:17:11 -04:00
if ARGV.length < 2
Sidekiqctl.print_usage
else
stage = ARGV[0]
pidfile = ARGV[1]
timeout = ARGV[2].to_i
timeout = Sidekiqctl::DEFAULT_TIMEOUT if timeout == 0
2014-03-13 13:17:11 -04:00
Sidekiqctl.new(stage, pidfile, timeout)
end