From c31c3df39a4d55f4ed198ad2ad65b4ecdc2c673d Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sat, 22 Sep 2007 15:02:58 -0700 Subject: [PATCH] add info to degrading lambda, tested before and after behaviors --- History.txt | 2 ++ lib/god/behavior.rb | 4 ++++ lib/god/conditions/degrading_lambda.rb | 9 +++++++-- lib/god/watch.rb | 9 ++++++++- test/configs/daemon_events/daemon_events.god | 1 + test/configs/daemon_events/simple_server.rb | 2 ++ test/helper.rb | 6 ++++++ test/test_watch.rb | 16 ++++++++++++++++ 8 files changed, 46 insertions(+), 3 deletions(-) diff --git a/History.txt b/History.txt index bb577fa..b12a345 100644 --- a/History.txt +++ b/History.txt @@ -16,6 +16,8 @@ * Explain what's going on when attempting to rebind to an in-use port * Add -b option to god binary to auto-bind to an unused port * Add `god quit` to stop god without stopping any tasks + * Make self-daemonized Watch commands synchronous (as they should be) + * Allow self-daemonized Watches to specify a log (could be useful) * New Conditions * Flapping < TriggerCondition - trigger on state change * HttpResponseCode < PollCondition - trigger on http response code or timeout (thx scott becker) diff --git a/lib/god/behavior.rb b/lib/god/behavior.rb index af25296..478e6ef 100644 --- a/lib/god/behavior.rb +++ b/lib/god/behavior.rb @@ -16,6 +16,10 @@ module God rescue NameError raise NoSuchBehaviorError.new("No Behavior found with the class name God::Behaviors::#{sym}") end + + def valid? + true + end ####### diff --git a/lib/god/conditions/degrading_lambda.rb b/lib/god/conditions/degrading_lambda.rb index 7a35f3b..5bb97dd 100644 --- a/lib/god/conditions/degrading_lambda.rb +++ b/lib/god/conditions/degrading_lambda.rb @@ -15,18 +15,23 @@ module God valid &= complain("Attribute 'lambda' must be specified", self) if self.lambda.nil? valid end - + def test puts "Calling test. Interval at #{self.interval}" @original_interval ||= self.interval unless pass? - return true if @tries == 2 + if @tries == 2 + self.info = "lambda condition was satisfied" + return true + end self.interval = self.interval / 2.0 @tries += 1 else @tries = 0 self.interval = @original_interval end + + self.info = "lambda condition was not satisfied" false end diff --git a/lib/god/watch.rb b/lib/god/watch.rb index ab5b74c..b19b61b 100644 --- a/lib/god/watch.rb +++ b/lib/god/watch.rb @@ -140,7 +140,14 @@ module God # after after_items = self.behaviors after_items += [condition] if condition - after_items.each { |b| b.send("after_#{action}") } + after_items.each do |b| + info = b.send("after_#{action}") + if info + msg = "#{self.name} after_#{action}: #{info} (#{b.base_name})" + Syslog.debug(msg) + LOG.log(self, :info, msg) + end + end end ########################################################################### diff --git a/test/configs/daemon_events/daemon_events.god b/test/configs/daemon_events/daemon_events.god index 304f5b0..f1b5b66 100644 --- a/test/configs/daemon_events/daemon_events.god +++ b/test/configs/daemon_events/daemon_events.god @@ -4,6 +4,7 @@ God.watch do |w| w.start = '/usr/local/bin/ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + ' start' w.stop = '/usr/local/bin/ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + ' stop' w.pid_file = '/var/run/daemon-events.pid' + w.log = File.join(File.dirname(__FILE__), 'daemon_events.log') w.behavior(:clean_pid_file) diff --git a/test/configs/daemon_events/simple_server.rb b/test/configs/daemon_events/simple_server.rb index f8d02f6..2f417c3 100644 --- a/test/configs/daemon_events/simple_server.rb +++ b/test/configs/daemon_events/simple_server.rb @@ -1,6 +1,8 @@ require 'rubygems' require 'daemons' +puts 'simple server ahoy!' + Daemons.run_proc('daemon-events', {:dir_mode => :system}) do loop { puts 'server'; sleep 1 } end \ No newline at end of file diff --git a/test/helper.rb b/test/helper.rb index c989e5c..ecf170e 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -51,6 +51,12 @@ module God module Behaviors class FakeBehavior < Behavior + def before_start + 'foo' + end + def after_start + 'bar' + end end end diff --git a/test/test_watch.rb b/test/test_watch.rb index 99ffa73..d4d01ce 100644 --- a/test/test_watch.rb +++ b/test/test_watch.rb @@ -233,6 +233,22 @@ class TestWatch < Test::Unit::TestCase no_stdout { @watch.call_action(c, :start) } end + def test_call_action_should_call_before_start_when_behavior_has_that + @watch.behavior(:fake_behavior) + c = Conditions::FakePollCondition.new + God::Process.any_instance.expects(:call_action).with(:start) + Behaviors::FakeBehavior.any_instance.expects(:before_start) + no_stdout { @watch.call_action(c, :start) } + end + + def test_call_action_should_call_after_start_when_behavior_has_that + @watch.behavior(:fake_behavior) + c = Conditions::FakePollCondition.new + God::Process.any_instance.expects(:call_action).with(:start) + Behaviors::FakeBehavior.any_instance.expects(:after_start) + no_stdout { @watch.call_action(c, :start) } + end + # canonical_hash_form def test_canonical_hash_form_should_convert_symbol_to_hash -- 2.11.4.GIT