From 9af68c558ed11efaf2ad733a626e79f4bf91de20 Mon Sep 17 00:00:00 2001 From: Tom Werner Date: Thu, 6 Sep 2007 12:50:20 -0700 Subject: [PATCH] fix terminate's premature death and add threading --- bin/god | 3 ++ lib/god.rb | 8 +++-- lib/god/hub.rb | 80 +++++++++++++++++++++--------------------- lib/god/process.rb | 2 +- test/configs/stress/stress.god | 4 +-- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/bin/god b/bin/god index bfd370c..13e0eb5 100755 --- a/bin/god +++ b/bin/god @@ -132,9 +132,12 @@ elsif command = ARGV[0] exit!(0) end elsif command == 'terminate' + t = Thread.new { loop { STDOUT.print('.'); STDOUT.flush; sleep(1) } } if server.stop_all + t.kill; STDOUT.puts puts 'Stopped all watches' else + t.kill; STDOUT.puts puts 'Could not stop all watches within 10 seconds' end diff --git a/lib/god.rb b/lib/god.rb index b9b3694..e0f6772 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -194,9 +194,11 @@ module God end def self.stop_all - self.watches.each do |name, w| - w.unmonitor if w.state - w.action(:stop) if w.alive? + self.watches.sort.each do |name, w| + Thread.new do + w.unmonitor if w.state + w.action(:stop) if w.alive? + end end 10.times do diff --git a/lib/god/hub.rb b/lib/god/hub.rb index 3a0a9d5..70313ff 100644 --- a/lib/god/hub.rb +++ b/lib/god/hub.rb @@ -51,48 +51,48 @@ module God # it's possible that the timer will trigger an event before it can be cleared # by an exiting metric, in which case it should be ignored - exit if metric.nil? - - watch = metric.watch - - watch.mutex.synchronize do - # run the test - result = condition.test - - # log - msg = watch.name + ' ' + condition.class.name + " [#{result}] " + metric.destination.inspect - Syslog.debug(msg) - LOG.log(watch, :info, msg) - - # after-condition - condition.after - - # get the destination - dest = - if result && condition.transition - # condition override - condition.transition - else - # regular - metric.destination[result] - end + unless metric.nil? + watch = metric.watch - # transition or reschedule - if dest - # transition - begin - watch.move(dest) - rescue EventRegistrationFailedError - msg = watch.name + ' Event registration failed, moving back to previous state' - Syslog.debug(msg) - LOG.log(watch, :info, msg) - - dest = watch.state - retry + watch.mutex.synchronize do + # run the test + result = condition.test + + # log + msg = watch.name + ' ' + condition.class.name + " [#{result}] " + metric.destination.inspect + Syslog.debug(msg) + LOG.log(watch, :info, msg) + + # after-condition + condition.after + + # get the destination + dest = + if result && condition.transition + # condition override + condition.transition + else + # regular + metric.destination[result] + end + + # transition or reschedule + if dest + # transition + begin + watch.move(dest) + rescue EventRegistrationFailedError + msg = watch.name + ' Event registration failed, moving back to previous state' + Syslog.debug(msg) + LOG.log(watch, :info, msg) + + dest = watch.state + retry + end + else + # reschedule + Timer.get.schedule(condition) end - else - # reschedule - Timer.get.schedule(condition) end end rescue => e diff --git a/lib/god/process.rb b/lib/god/process.rb index 4c53a5c..d06cd29 100644 --- a/lib/god/process.rb +++ b/lib/god/process.rb @@ -143,7 +143,7 @@ module God STDOUT.reopen "/dev/null", "a" end STDERR.reopen STDOUT - + exec command unless command.empty? end puts pid.to_s diff --git a/test/configs/stress/stress.god b/test/configs/stress/stress.god index e8c8177..acb2ad7 100644 --- a/test/configs/stress/stress.god +++ b/test/configs/stress/stress.god @@ -1,7 +1,7 @@ -(1..20).each do |i| +('01'..'20').each do |i| God.watch do |w| w.name = "stress-#{i}" - w.start = 'ruby ' + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) + w.start = "ruby " + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) w.interval = 1 w.grace = 2 w.group = 'test' -- 2.11.4.GIT