Skip to content

Instantly share code, notes, and snippets.

@HendrikLouw
Created April 3, 2012 11:38
Show Gist options
  • Save HendrikLouw/2291204 to your computer and use it in GitHub Desktop.
Save HendrikLouw/2291204 to your computer and use it in GitHub Desktop.
Resque restart when a file is touched ( like passenger restart )
# Can't remember where I found this, backing it up here.
# Use bottom script like so
# on.condition(:restart_file_touched) do |c|
# c.interval = 5.seconds
# c.restart_file = File.join(rails_root, 'tmp', 'restart.txt')
# end
# restart.god
module God
module Conditions
class RestartFileTouched < PollCondition
attr_accessor :restart_file
def initialize
super
end
def process_start_time
tmp = Time.parse(`ps -o lstart -p #{self.watch.pid} --no-heading`)
puts "Process time: #{tmp}"
tmp
end
def restart_file_modification_time
tmp = File.mtime(self.restart_file)
puts "Restart file:#{self.restart_file} #{tmp}"
tmp
end
def valid?
valid = true
valid &= complain("Attribute 'restart_file' must be specified", self) if self.restart_file.nil?
valid
end
def test
process_start_time < restart_file_modification_time
end
end
end
end
#Example resque.god that uses restart
rails_env = ENV['RAILS_ENV'] || "production"
rails_root = ENV['RAILS_ROOT']
num_workers = rails_env == 'production' ? 2 : 1
num_workers.times do |num|
God.watch do |w|
w.dir = "#{rails_root}"
w.name = "resque-#{num}"
w.group = 'resque'
w.interval = 30.seconds
w.env = {"QUEUE"=>"*", "RAILS_ENV"=>rails_env}
w.start = "/usr/local/rvm/gems/ruby-1.9.2-p290/bin/bundle exec rake -f #{rails_root}/Rakefile environment resque:work"
w.uid = 'user'
w.gid = 'user'
# restart if memory gets too high
w.transition(:up, :restart) do |on|
on.condition(:memory_usage) do |c|
c.above = 350.megabytes
c.times = 2
end
on.condition(:restart_file_touched) do |c|
c.interval = 5.seconds
c.restart_file = File.join(rails_root, 'tmp', 'restart.txt')
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment