Created
November 30, 2009 15:45
-
-
Save filiptepper/245507 to your computer and use it in GitHub Desktop.
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
rails_env = ENV['RAILS_ENV'] || "production" | |
rails_root = ENV['RAILS_ROOT'] || "/var/www/yourapp/current" | |
num_workers = 1 | |
num_workers.times do |num| | |
God.watch do |w| | |
w.name = "resque-#{num}" | |
w.group = 'resque' | |
w.interval = 30.seconds | |
w.start = "cd #{rails_root} && env QUEUE=* RAILS_ENV=#{rails_env} /usr/bin/rake -f #{rails_root}/Rakefile environment resque:work" | |
w.stop = lambda { | |
begin | |
pid = `cat /var/run/god/resque-#{num}.pid` | |
`kill -INT #{pid}` | |
`ps -e -o pid,command | grep "[r]esque: Waiting for"`.each do |line| | |
parts = line.split(" ") | |
::Process.kill("QUIT", parts[0].to_i) | |
end | |
rescue | |
end | |
} | |
# retart if memory gets too high | |
w.transition(:up, :restart) do |on| | |
on.condition(:memory_usage) do |c| | |
c.above = 200.megabytes | |
c.times = 2 | |
end | |
end | |
# determine the state on startup | |
w.transition(:init, { true => :up, false => :start }) do |on| | |
on.condition(:process_running) do |c| | |
c.running = true | |
end | |
end | |
# determine when process has finished starting | |
w.transition([:start, :restart], :up) do |on| | |
on.condition(:process_running) do |c| | |
c.running = true | |
c.interval = 5.seconds | |
end | |
# failsafe | |
on.condition(:tries) do |c| | |
c.times = 5 | |
c.transition = :start | |
c.interval = 5.seconds | |
end | |
end | |
# start if process is not running | |
w.transition(:up, :start) do |on| | |
on.condition(:process_running) do |c| | |
c.running = false | |
end | |
end | |
end | |
end | |
WORKER_TIMEOUT = 60 * 10 # 10 minutes | |
Thread.new do | |
loop do | |
begin | |
`ps -e -o pid,command | grep [r]esque`.split("\n").each do |line| | |
parts = line.split(' ') | |
next if parts[-2] != "at" | |
started = parts[-1].to_i | |
elapsed = Time.now - Time.at(started) | |
if elapsed >= WORKER_TIMEOUT | |
::Process.kill('USR1', parts[0].to_i) | |
end | |
end | |
rescue | |
# don't die because of stupid exceptions | |
nil | |
end | |
sleep 30 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment