From fcb37b024fd6aac72568bd6458effdd0dad6f089 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Fri, 28 Sep 2007 23:35:27 -0700 Subject: [PATCH] deprecate God.init in favor of directly setting options --- History.txt | 2 ++ lib/god.rb | 43 +++++++++++++++++++++--------- test/configs/child_events/child_events.god | 4 +++ test/test_god.rb | 14 ++++++---- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/History.txt b/History.txt index d21aacb..9e82fc0 100644 --- a/History.txt +++ b/History.txt @@ -18,6 +18,8 @@ * Make self-daemonized Watch commands synchronous (as they should be) * Allow self-daemonized Watches to specify a log (could be useful) * Check for existence of config file if specified + * Robustify `god load` and report errors back to the command issuer + * Warn when `god load` tries to set global options * 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.rb b/lib/god.rb index 8d083b0..e11eccf 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -100,6 +100,29 @@ module Kernel end end +class Module + def safe_attr_accessor(*args) + args.each do |arg| + define_method((arg.to_s + "=").intern) do |other| + if !self.running && self.inited + abort "God.#{arg} must be set before any Tasks are defined" + end + + if self.running && self.inited + LOG.log(nil, :warn, "God.#{arg} can't be set while god is running") + return + end + + instance_variable_set(('@' + arg.to_s).intern, other) + end + + define_method(arg) do + instance_variable_get(('@' + arg.to_s).intern) + end + end + end +end + module God VERSION = '0.5.0' @@ -110,11 +133,11 @@ module God class << self # user configurable - attr_accessor :host, - :port, - :allow, - :log_buffer_size, - :pid_file_directory + safe_attr_accessor :host, + :port, + :allow, + :log_buffer_size, + :pid_file_directory # internal attr_accessor :inited, @@ -127,14 +150,8 @@ module God :contact_groups end + # deprecated def self.init - if self.inited - abort "God.init must be called before any Watches" - end - - self.internal_init - - # yield to the config file yield self if block_given? end @@ -318,7 +335,7 @@ module God end def self.terminate - exit + exit!(0) end def self.status diff --git a/test/configs/child_events/child_events.god b/test/configs/child_events/child_events.god index 0e6a791..67bfb50 100644 --- a/test/configs/child_events/child_events.god +++ b/test/configs/child_events/child_events.god @@ -1,3 +1,7 @@ +God.init do |god| + god.host = '127.0.0.1' +end + God.watch do |w| w.name = "child-events" w.interval = 5.seconds diff --git a/test/test_god.rb b/test/test_god.rb index cf023a3..5f4c4cd 100644 --- a/test/test_god.rb +++ b/test/test_god.rb @@ -12,25 +12,27 @@ class TestGod < Test::Unit::TestCase Timer.get.timer.kill end - # init + # internal_init def test_init_should_initialize_watches_to_empty_array - God.init { } + God.internal_init { } assert_equal Hash.new, God.watches end + # init + def test_init_should_abort_if_called_after_watch God.watch { |w| w.name = 'foo'; w.start = 'bar' } assert_abort do - God.init { } + God.init { |g| g.pid_file_directory = 'foo' } end end # pid_file_directory def test_pid_file_directory_should_return_default_if_not_set_explicitly - God.init + God.internal_init assert_equal '/var/run/god', God.pid_file_directory end @@ -279,6 +281,7 @@ class TestGod < Test::Unit::TestCase God.watch { |w| w.name = 'foo'; w.start = 'bar' } w = God.watches['foo'] + w.state = :up w.expects(:unmonitor).returns(w) w.expects(:action).with(:stop) God.control('foo', 'stop') @@ -288,6 +291,7 @@ class TestGod < Test::Unit::TestCase God.watch { |w| w.name = 'foo'; w.start = 'bar' } w = God.watches['foo'] + w.state = :up w.expects(:unmonitor).returns(w) God.control('foo', 'unmonitor') end @@ -324,7 +328,7 @@ class TestGod < Test::Unit::TestCase # terminate def test_terminate_should_exit - God.expects(:exit) + God.expects(:exit!) God.terminate end -- 2.11.4.GIT