From 44a09e1fe67d9a8b6b88a719d6992c51c9610474 Mon Sep 17 00:00:00 2001 From: Tom Werner Date: Mon, 1 Oct 2007 18:57:15 -0700 Subject: [PATCH] put watch back into previous state after a god load --- bin/god | 21 +++++++++++++-------- examples/events.god | 5 +++-- lib/god.rb | 19 +++++++++++++++---- test/configs/daemon_events/daemon_events.god | 6 ++++-- test/configs/daemon_events/simple_server_stop.rb | 2 ++ 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/bin/god b/bin/god index 16dfe05..0641d25 100755 --- a/bin/god +++ b/bin/god @@ -107,18 +107,19 @@ begin file = ARGV[1] puts "Sending '#{command}' command" + puts unless File.exist?(file) abort "File not found: #{file}" end - watches, errors = *server.running_load(File.read(file), File.expand_path(file)) + names, errors = *server.running_load(File.read(file), File.expand_path(file)) # output response - unless watches.empty? - puts 'The following watches were affected:' - watches.each do |w| - puts ' ' + w.name + unless names.empty? + puts 'The following tasks were affected:' + names.each do |w| + puts ' ' + w end end @@ -184,9 +185,13 @@ begin # output response t.kill; STDOUT.puts - puts 'The following watches were affected:' - watches.each do |w| - puts ' ' + w.name + unless watches.empty? + puts 'The following watches were affected:' + watches.each do |w| + puts ' ' + w + end + else + puts 'No matching task or group' end rescue God::InvalidCommandError t.kill rescue nil; STDOUT.puts diff --git a/examples/events.god b/examples/events.god index d1142f4..76d45da 100644 --- a/examples/events.god +++ b/examples/events.god @@ -4,7 +4,7 @@ # Run with: # god -c /path/to/events.god -RAILS_ROOT = "/Users/tom/dev/git/helloworld" +RAILS_ROOT = ENV['GOD_TEST_RAILS_ROOT'] God.watch do |w| w.name = "local-3000" @@ -13,6 +13,7 @@ God.watch do |w| w.stop = "mongrel_rails stop -P #{RAILS_ROOT}/log/mongrel.pid -c #{RAILS_ROOT}" w.pid_file = File.join(RAILS_ROOT, "log/mongrel.pid") w.log = File.join(RAILS_ROOT, "log/commands.log") + w.autostart = false # clean pid files before start if necessary w.behavior(:clean_pid_file) @@ -60,7 +61,7 @@ God.watch do |w| c.host = 'localhost' c.port = '3000' c.path = '/' - c.code_is_not = 200 + c.code_is = 500 c.timeout = 10.seconds c.times = [3, 5] end diff --git a/lib/god.rb b/lib/god.rb index 23a8148..ddbc17e 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -143,6 +143,7 @@ module God attr_accessor :inited, :running, :pending_watches, + :pending_watch_states, :server, :watches, :groups, @@ -163,6 +164,7 @@ module God self.watches = {} self.groups = {} self.pending_watches = [] + self.pending_watch_states = {} self.contacts = {} self.contact_groups = {} @@ -203,6 +205,7 @@ module God # prepare for the reload existing_watch = self.watches[t.name] if self.running && existing_watch + self.pending_watch_states[existing_watch.name] = existing_watch.state self.unwatch(existing_watch) end @@ -244,7 +247,7 @@ module God def self.unwatch(watch) # unmonitor - watch.unmonitor + watch.unmonitor unless watch.state == :unmonitored # unregister watch.unregister! @@ -322,7 +325,7 @@ module God jobs.each { |j| j.join } - watches + watches.map { |x| x.name } end def self.stop_all @@ -370,9 +373,16 @@ module God CONFIG_FILE.replace(filename) eval(code, nil, filename) - self.pending_watches.each { |w| w.monitor if w.autostart? } + self.pending_watches.each do |w| + if previous_state = self.pending_watch_states[w.name] + w.monitor unless previous_state == :unmonitored + else + w.monitor if w.autostart? + end + end watches = self.pending_watches.dup self.pending_watches.clear + self.pending_watch_states.clear rescue Exception => e # don't ever let running_load take down god errors << LOG.finish_capture @@ -383,7 +393,8 @@ module God end end - [watches, errors] + names = watches.map { |x| x.name } + [names, errors] end def self.load(glob) diff --git a/test/configs/daemon_events/daemon_events.god b/test/configs/daemon_events/daemon_events.god index 8ea57d5..3a91f98 100644 --- a/test/configs/daemon_events/daemon_events.god +++ b/test/configs/daemon_events/daemon_events.god @@ -1,10 +1,12 @@ God.watch do |w| w.name = "daemon-events" w.interval = 5.seconds - 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_stop.rb]) + w.start = 'ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + ' start' + w.stop = 'ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server_stop.rb]) w.pid_file = '/var/run/daemon-events.pid' w.log = File.join(File.dirname(__FILE__), 'daemon_events.log') + w.uid = 'tom' + w.gid = 'tom' w.behavior(:clean_pid_file) diff --git a/test/configs/daemon_events/simple_server_stop.rb b/test/configs/daemon_events/simple_server_stop.rb index 22d03bb..69eae17 100644 --- a/test/configs/daemon_events/simple_server_stop.rb +++ b/test/configs/daemon_events/simple_server_stop.rb @@ -3,5 +3,7 @@ sleep 1 end +p ENV + command = '/usr/local/bin/ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + ' stop' system(command) \ No newline at end of file -- 2.11.4.GIT