2012-03-27 23:19:24 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
|
2012-03-28 13:40:46 -04:00
|
|
|
class Sidekiqctl
|
2014-03-13 13:17:11 -04:00
|
|
|
DEFAULT_TIMEOUT = 10
|
2012-03-27 23:19:24 -04:00
|
|
|
|
2012-03-28 13:40:46 -04:00
|
|
|
attr_reader :stage, :pidfile, :timeout
|
2012-03-27 23:19:24 -04:00
|
|
|
|
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
|
|
|
|
|
2012-03-28 13:40:46 -04:00
|
|
|
def initialize(stage, pidfile, timeout)
|
|
|
|
@stage = stage
|
|
|
|
@pidfile = pidfile
|
|
|
|
@timeout = timeout
|
2012-03-27 23:19:24 -04:00
|
|
|
|
2013-01-07 16:17:29 -05:00
|
|
|
done('No pidfile given', :error) if !pidfile
|
2014-02-18 14:23:15 -05:00
|
|
|
done("Pidfile #{pidfile} does not exist", :warn) if !File.exist?(pidfile)
|
2013-01-07 16:17:29 -05:00
|
|
|
done('Invalid pidfile content', :error) if pid == 0
|
2012-03-27 23:19:24 -04:00
|
|
|
|
2012-03-28 13:40:46 -04:00
|
|
|
fetch_process
|
2012-03-28 12:45:58 -04:00
|
|
|
|
2012-03-27 23:19:24 -04:00
|
|
|
begin
|
2012-03-28 13:40:46 -04:00
|
|
|
send(stage)
|
|
|
|
rescue NoMethodError
|
2013-01-07 16:17:29 -05:00
|
|
|
done 'Invalid control command', :error
|
2012-03-27 23:19:24 -04:00
|
|
|
end
|
|
|
|
end
|
2012-03-28 12:45:58 -04:00
|
|
|
|
2012-03-28 13:40:46 -04:00
|
|
|
def fetch_process
|
|
|
|
Process.getpgid(pid)
|
|
|
|
rescue Errno::ESRCH
|
2013-01-07 16:17:29 -05:00
|
|
|
done "Process doesn't exist", :error
|
2012-03-28 13:40:46 -04:00
|
|
|
end
|
|
|
|
|
2013-01-07 16:17:29 -05:00
|
|
|
def done(msg, error = nil)
|
2012-03-28 13:40:46 -04:00
|
|
|
puts msg
|
2013-01-07 16:17:29 -05:00
|
|
|
exit(exit_signal(error))
|
|
|
|
end
|
|
|
|
|
|
|
|
def exit_signal(error)
|
|
|
|
(error == :error) ? 1 : 0
|
2012-03-28 13:40:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pid
|
2014-02-18 14:23:15 -05:00
|
|
|
@pid ||= File.read(pidfile).to_i
|
2012-03-28 13:40:46 -04:00
|
|
|
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
|
2012-03-28 13:40:46 -04:00
|
|
|
done 'Sidekiq shut down forcefully.'
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown
|
|
|
|
quiet
|
|
|
|
stop
|
|
|
|
end
|
2012-03-28 12:45:58 -04:00
|
|
|
end
|
2012-03-28 13:40:46 -04:00
|
|
|
|
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
|
2012-03-28 13:40:46 -04:00
|
|
|
|
2014-03-13 13:17:11 -04:00
|
|
|
Sidekiqctl.new(stage, pidfile, timeout)
|
|
|
|
end
|