Skip to content

Instantly share code, notes, and snippets.

@digitalhobbit
Created December 8, 2009 15:29
Show Gist options
  • Save digitalhobbit/251724 to your computer and use it in GitHub Desktop.
Save digitalhobbit/251724 to your computer and use it in GitHub Desktop.
# Based on example from <http://railscasts.com/episodes/130-monitoring-with-god>
# Use static root, as we're currently not reloading God config during deployments
APP_ROOT = '/var/www/twatcher/current'
APP_ENV = 'production'
def generic_monitoring(w, options = {})
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 10.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = options[:memory_limit]
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = options[:cpu_limit]
c.times = 5
end
end
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.transition = :unmonitored
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
God.watch do |w|
w.name = "redis"
#w.group = "redis"
w.interval = 60.seconds
w.start = "/usr/local/bin/redis-server #{APP_ROOT}/config/redis.conf"
w.stop = "kill `cat /var/run/redis.pid`"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = "/var/run/redis.pid"
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 30.percent, :memory_limit => 30.megabytes)
end
God.watch do |w|
w.name = "twitter_filter"
w.interval = 60.seconds
# For some reason this only works when we first cd, then use a relative path. No idea why.
w.start = "cd #{APP_ROOT}; RACK_ENV=#{APP_ENV} bin/twitter_filter.rb start"
w.stop = "cd #{APP_ROOT}; RACK_ENV=#{APP_ENV} bin/twitter_filter.rb stop"
w.start_grace = 10.seconds
w.restart_grace = 10.seconds
w.pid_file = "#{APP_ROOT}/log/twitter_filter.pid"
w.behavior(:clean_pid_file)
generic_monitoring(w, :cpu_limit => 30.percent, :memory_limit => 50.megabytes)
end
# I'm using the Daemons gem to daemonize the app. Will share the whole app soon, but basically like this:
daemon_options = {
:dir_mode => :normal,
:dir => File.join(File.dirname(__FILE__), '..', 'log'),
:log_output => true
}
Daemons.run_proc('twitter_filter', daemon_options) do
# Actual twitter filter code goes here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment