Created
August 27, 2012 03:54
-
-
Save mirakui/3485364 to your computer and use it in GitHub Desktop.
Restarting Unicorn Synchronously
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "optparse" | |
def usage | |
STDERR.puts "usage: #{$0} /path/to/unicorn.pid [-t timeout_sec]" | |
end | |
timeout = 60 | |
pidfile = ARGV.shift | |
unless pidfile | |
usage | |
exit 1 | |
end | |
unless File.exist?(pidfile) | |
STDERR.puts "PID file doesn't exist: #{pidfile}" | |
exit 1 | |
end | |
opt = OptionParser.new(ARGV) | |
opt.on("-t timeout_sec") {|t| timeout = t.to_i } | |
opt.parse! | |
oldbin = "#{pidfile}.oldbin" | |
old_pid = open(pidfile).read.to_i | |
new_pid = nil | |
if File.exist?(oldbin) | |
STDERR.puts "Still restarting" | |
exit 1 | |
else | |
Process.kill :USR2, old_pid | |
end | |
print "Restarting Unicorn" | |
STDOUT.flush | |
timeout_count = 0 | |
loop do | |
if timeout_count > timeout | |
STDERR.puts "Timeout" | |
exit 1 | |
end | |
if new_pid | |
if File.exist?(oldbin) | |
print '.' | |
STDOUT.flush | |
else | |
if open(pidfile).read.to_i == old_pid | |
puts "Failure" | |
else | |
puts "Success" | |
end | |
break | |
end | |
else | |
if File.exist?(pidfile) | |
new_pid = open(pidfile).read.to_i | |
end | |
end | |
sleep 1 | |
timeout_count += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment