Permalink
Browse files

added cpu_usage condition and local.god example

  • Loading branch information...
1 parent a9c3f67 commit 018a13c44f13e85b4bfc6f8f072e5c3dcb09d6f1 @mojombo mojombo committed Jun 17, 2007
View
@@ -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
View
@@ -0,0 +1,38 @@
+# 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 = 60 # seconds
+
+ god.watch do |w|
+ 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"
+ w.grace = 5
+
+ pid_file = File.join(RAILS_ROOT, "log/mongrel.pid")
+
+ w.start_if do |start|
+ start.condition(:process_not_running) do |c|
+ c.pid_file = pid_file
+ end
+ end
+
+ w.restart_if do |restart|
+ restart.condition(:memory_usage) do |c|
+ c.pid_file = pid_file
+ 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
+ end
+end
View
@@ -11,6 +11,7 @@
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'
@@ -0,0 +1,42 @@
+module God
+ module Conditions
+
+ class CpuUsage < ProcessCondition
+ attr_accessor :above, :times
+
+ def initialize
+ super
+ self.above = nil
+ self.times = [1, 1]
+ end
+
+ def prepare
+ if self.times.kind_of?(Integer)
+ self.times = [self.times, self.times]
+ end
+
+ @timeline = Timeline.new(self.times[1])
+ end
+
+ def valid?
+ valid = super
+ valid = complain("You must specify the 'above' attribute for :memory_usage") if self.above.nil?
+ valid
+ end
+
+ def test
+ return false unless super
+ pid = File.open(self.pid_file).read.strip
+ process = System::Process.new(pid)
+ @timeline.push(process.percent_cpu)
+ if @timeline.select { |x| x > self.above }.size < self.times.first
+ return true
+ else
+ @timeline.clear
+ return false
+ end
+ end
+ end
+
+ end
+end
@@ -11,8 +11,6 @@ def initialize
end
def prepare
- p self.times.class
-
if self.times.kind_of?(Integer)
self.times = [self.times, self.times]
end
@@ -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
View
@@ -8,7 +8,7 @@
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 @@
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

0 comments on commit 018a13c

Please sign in to comment.