bin/sidekiqctl in sidekiq-0.10.0 vs bin/sidekiqctl in sidekiq-0.10.1
- old
+ new
@@ -1,43 +1,74 @@
#!/usr/bin/env ruby
require 'fileutils'
-stage = ARGV[0]
-pidfile = ARGV[1]
-timeout = ARGV[2].to_i
-timeout = 10 if timeout == 0
+class Sidekiqctl
-def done(msg)
- puts msg
- exit(0)
-end
+ attr_reader :stage, :pidfile, :timeout
-done 'No pidfile given' if !pidfile
-done 'Pidfile does not exist' if !File.exist?(pidfile)
+ def initialize(stage, pidfile, timeout)
+ @stage = stage
+ @pidfile = pidfile
+ @timeout = timeout
-pid = File.read(pidfile).to_i
-done 'Invalid pidfile content' if pid == 0
+ done 'No pidfile given' if !pidfile
+ done 'Pidfile does not exist' if !File.exist?(pidfile)
+ done 'Invalid pidfile content' if pid == 0
-begin
- Process.getpgid(pid)
-rescue Errno::ESRCH
- done "Process doesn't exist"
-end
+ fetch_process
-case stage
-when 'quiet'
- `kill -USR1 #{pid}`
-when 'stop'
- `kill -TERM #{pid}`
- timeout.times do
begin
- Process.getpgid(pid)
- rescue Errno::ESRCH
- FileUtils.rm_f pidfile
- done 'Sidekiq shut down gracefully.'
+ send(stage)
+ rescue NoMethodError
+ done 'Invalid control command'
end
- sleep 1
end
- `kill -9 #{pid}`
- done 'Sidekiq shut down forcefully.'
+
+ def fetch_process
+ Process.getpgid(pid)
+ rescue Errno::ESRCH
+ done "Process doesn't exist"
+ end
+
+ def done(msg)
+ puts msg
+ exit(0)
+ end
+
+ def 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}`
+ FileUtils.rm_f pidfile
+ done 'Sidekiq shut down forcefully.'
+ end
+
+ def shutdown
+ quiet
+ stop
+ end
+
end
+
+stage = ARGV[0]
+pidfile = ARGV[1]
+timeout = ARGV[2].to_i
+timeout = 10 if timeout == 0
+
+Sidekiqctl.new(stage, pidfile, timeout)