From 018a13c44f13e85b4bfc6f8f072e5c3dcb09d6f1 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sun, 17 Jun 2007 00:12:06 -0700 Subject: [PATCH] added cpu_usage condition and local.god example --- Manifest.txt | 6 ++++++ test/configs/real.rb => examples/local.god | 17 +++++++++++------ lib/god.rb | 1 + lib/god/conditions/{memory_usage.rb => cpu_usage.rb} | 6 ++---- lib/god/conditions/memory_usage.rb | 2 -- lib/god/process_condition.rb | 2 ++ test/configs/real.rb | 10 ++++++++-- 7 files changed, 30 insertions(+), 14 deletions(-) copy test/configs/real.rb => examples/local.god (62%) copy lib/god/conditions/{memory_usage.rb => cpu_usage.rb} (88%) diff --git a/Manifest.txt b/Manifest.txt index 53985d4..f194e92 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -4,12 +4,17 @@ README.txt Rakefile bin/god examples/gravatar.god +examples/local.god lib/god.rb lib/god/base.rb lib/god/condition.rb +lib/god/conditions/cpu_usage.rb +lib/god/conditions/memory_usage.rb lib/god/conditions/process_not_running.rb +lib/god/conditions/timeline.rb lib/god/errors.rb lib/god/meddle.rb +lib/god/process_condition.rb lib/god/system/process.rb lib/god/watch.rb test/configs/no_watches.rb @@ -19,4 +24,5 @@ test/helper.rb test/test_condition.rb test/test_meddle.rb test/test_system_process.rb +test/test_timeline.rb test/test_watch.rb diff --git a/test/configs/real.rb b/examples/local.god similarity index 62% copy from test/configs/real.rb copy to examples/local.god index ced8948..8c8c7dc 100644 --- a/test/configs/real.rb +++ b/examples/local.god @@ -1,14 +1,13 @@ -if $0 == __FILE__ - require File.join(File.dirname(__FILE__), *%w[.. .. lib god]) -end +# This example shows how you might keep a local development Rails server up +# and running on your Mac. RAILS_ROOT = "/Users/tom/dev/gravatar2" God.meddle do |god| - god.interval = 5 # seconds + god.interval = 60 # seconds god.watch do |w| - w.name = "gravatar2-mongrel-3000" + w.name = "local-3000" w.cwd = RAILS_ROOT w.start = "mongrel_rails start -P ./log/mongrel.pid -d" w.stop = "mongrel_rails stop -P ./log/mongrel.pid" @@ -25,7 +24,13 @@ God.meddle do |god| w.restart_if do |restart| restart.condition(:memory_usage) do |c| c.pid_file = pid_file - c.above = 10 # kb + c.above = (50 * 1024) # 50mb + c.times = [3, 5] + end + + restart.condition(:cpu_usage) do |c| + c.pid_file = pid_file + c.above = 10 # percent c.times = [3, 5] end end diff --git a/lib/god.rb b/lib/god.rb index 44152c3..e351607 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -11,6 +11,7 @@ require 'god/process_condition' require 'god/conditions/timeline' require 'god/conditions/process_not_running' require 'god/conditions/memory_usage' +require 'god/conditions/cpu_usage' require 'god/watch' require 'god/meddle' diff --git a/lib/god/conditions/memory_usage.rb b/lib/god/conditions/cpu_usage.rb similarity index 88% copy from lib/god/conditions/memory_usage.rb copy to lib/god/conditions/cpu_usage.rb index a5b4bc1..d5a10ec 100644 --- a/lib/god/conditions/memory_usage.rb +++ b/lib/god/conditions/cpu_usage.rb @@ -1,7 +1,7 @@ module God module Conditions - class MemoryUsage < ProcessCondition + class CpuUsage < ProcessCondition attr_accessor :above, :times def initialize @@ -11,8 +11,6 @@ module God end def prepare - p self.times.class - if self.times.kind_of?(Integer) self.times = [self.times, self.times] end @@ -30,7 +28,7 @@ module God return false unless super pid = File.open(self.pid_file).read.strip process = System::Process.new(pid) - @timeline.push(process.memory) + @timeline.push(process.percent_cpu) if @timeline.select { |x| x > self.above }.size < self.times.first return true else diff --git a/lib/god/conditions/memory_usage.rb b/lib/god/conditions/memory_usage.rb index a5b4bc1..c1515b4 100644 --- a/lib/god/conditions/memory_usage.rb +++ b/lib/god/conditions/memory_usage.rb @@ -11,8 +11,6 @@ module God end def prepare - p self.times.class - if self.times.kind_of?(Integer) self.times = [self.times, self.times] end diff --git a/lib/god/process_condition.rb b/lib/god/process_condition.rb index 97f5e76..5b6a533 100644 --- a/lib/god/process_condition.rb +++ b/lib/god/process_condition.rb @@ -1,5 +1,7 @@ module God + # This abstract class makes it easy for subclassed Conditions to deal + # with commong process related tasks, like pid file cleanup class ProcessCondition < Condition attr_accessor :pid_file, :clean diff --git a/test/configs/real.rb b/test/configs/real.rb index ced8948..79ff14a 100644 --- a/test/configs/real.rb +++ b/test/configs/real.rb @@ -8,7 +8,7 @@ God.meddle do |god| god.interval = 5 # seconds god.watch do |w| - w.name = "gravatar2-mongrel-3000" + w.name = "local-3000" w.cwd = RAILS_ROOT w.start = "mongrel_rails start -P ./log/mongrel.pid -d" w.stop = "mongrel_rails stop -P ./log/mongrel.pid" @@ -25,7 +25,13 @@ God.meddle do |god| w.restart_if do |restart| restart.condition(:memory_usage) do |c| c.pid_file = pid_file - c.above = 10 # kb + c.above = (50 * 1024) # 50mb + c.times = [3, 5] + end + + restart.condition(:cpu_usage) do |c| + c.pid_file = pid_file + c.above = 10 # percent c.times = [3, 5] end end -- 2.11.4.GIT