Created
February 24, 2017 17:16
-
-
Save sunny/0567d2d31681feab90e15f8eb0e10625 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class Watcher | |
def watch | |
loop do | |
if unresponsive? && sleep(3) && unresponsive? | |
restart | |
sleep 20 | |
end | |
sleep 5 | |
end | |
end | |
private | |
SAFE = "HTTP/1.1 200 OK\r\n" | |
def unresponsive? | |
ret = `curl -q --max-time 10 -I http://localhost 2>/dev/null | head -n1` | |
return false if ret == SAFE | |
puts "[#{Time.new}] Not safe! Got: #{ret}" | |
true | |
end | |
def restart | |
puma_pid = current_puma_pid | |
puts "Puma pid found: #{puma_pid.inspect}" | |
kill(puma_pid) | |
start | |
puts "Done" | |
end | |
def current_puma_pid | |
pid = `ps aux | grep puma | grep -v grep | grep -v pumactl | grep -v wtf | tr -s ' ' | cut -d' ' -f2` | |
pid.chomp | |
end | |
def kill(pid) | |
kill_cmd = "cd ~/cults3d.com/current && kill -9 #{pid}" | |
puts "Killing puma via: #{kill_cmd}" | |
`#{kill_cmd}` | |
end | |
def start | |
start_cmd = "cd ~/cults3d.com/current && ./bin/puma -C config/puma.rb" | |
puts "Spawn command: #{start_cmd.inspect}" | |
`#{start_cmd}` | |
end | |
end | |
Watcher.new.watch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment