From 68f718f24a977df8e6a190a0ae68318b99ac9e7e Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sat, 3 Nov 2007 15:50:28 -0400 Subject: [PATCH] add shorthand selector for god log --- History.txt | 1 + lib/god.rb | 28 ++++++++++++++++++++++++++-- lib/god/conditions/flapping.rb | 28 ++++++++++++++++++++++++++++ lib/god/hub.rb | 4 ++-- lib/god/logger.rb | 4 ++-- test/test_god.rb | 11 +++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/History.txt b/History.txt index d6aa0ab..f7ef620 100644 --- a/History.txt +++ b/History.txt @@ -4,6 +4,7 @@ * Move Syslog calls into God::Logger and clean up all calling code * Remove god's pid file on user requested termination * Better handling and cleanup of DRb server's unix domain socket + * Allow shorthand for requesting a god log * New Conditions * DiskUsage < PollCondition - trigger if disk usage is above limit on mount [Rudy Desjardins] diff --git a/lib/god.rb b/lib/god.rb index 51715e7..06009f0 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -385,11 +385,13 @@ module God end def self.running_log(watch_name, since) - unless self.watches[watch_name] + matches = pattern_match(self.watches.keys, watch_name) + + unless matches.first raise NoSuchWatchError.new end - LOG.watch_log_since(watch_name, since) + LOG.watch_log_since(matches.first, since) end def self.running_load(code, filename) @@ -477,6 +479,28 @@ module God def self.at_exit self.start end + + # private + + # Match a shortened pattern against a list of String candidates. + # The pattern is expanded into a regular expression by + # inserting .* between each character. + # + # Examples + # + # list = %w{ foo bar bars } + # pattern = 'br' + # God.pattern_match(list, pattern) + # # => ['bar', 'bars'] + # + # Returns String[] + def self.pattern_match(list, pattern) + regex = pattern.split('').join('.*') + + list.select do |item| + item =~ Regexp.new(regex) + end + end end at_exit do diff --git a/lib/god/conditions/flapping.rb b/lib/god/conditions/flapping.rb index 0fcf4ac..78bf329 100644 --- a/lib/god/conditions/flapping.rb +++ b/lib/god/conditions/flapping.rb @@ -1,6 +1,34 @@ module God module Conditions + # Condition Symbol :flapping + # Type: Trigger + # + # Trigger when a Task transitions to or from a state or states a given number + # of times within a given period. + # + # Paramaters + # Required + # +times+ is the number of times that the Task must transition before + # triggering. + # +within+ is the number of seconds within which the Task must transition + # the specified number of times before triggering. You may use + # the sugar methods #seconds, #minutes, #hours, #days to clarify + # your code (see examples). + # --one or both of-- + # +from_state+ is the state (as a Symbol) from which the transition must occur. + # +to_state is the state (as a Symbol) to which the transition must occur. + # + # Optional: + # +retry_in+ is the number of seconds after which to re-monitor the Task after + # it has been disabled by the condition. + # +retry_times+ is the number of times after which to permanently unmonitor + # the Task. + # +retry_within+ is the number of seconds within which + # + # Examples + # + # Trigger if class Flapping < TriggerCondition attr_accessor :times, :within, diff --git a/lib/god/hub.rb b/lib/god/hub.rb index 4c4a810..6b8eb7a 100644 --- a/lib/god/hub.rb +++ b/lib/god/hub.rb @@ -162,11 +162,11 @@ module God if condition.info Array(condition.info).each do |condition_info| messages << "#{watch.name} #{status} #{condition_info} (#{condition.base_name})" - applog(watch, :info, messages.last % []) + applog(watch, :info, messages.last) end else messages << "#{watch.name} #{status} (#{condition.base_name})" - applog(watch, :info, messages.last % []) + applog(watch, :info, messages.last) end # log diff --git a/lib/god/logger.rb b/lib/god/logger.rb index 9375d0b..030e5c7 100644 --- a/lib/god/logger.rb +++ b/lib/god/logger.rb @@ -38,7 +38,7 @@ module God buf = StringIO.new templog = ::Logger.new(buf) templog.level = Logger::INFO - templog.send(level, text) + templog.send(level, text % []) @mutex.synchronize do @capture.puts(buf.string) if @capture self.logs[watch.name] << [Time.now, buf.string] if watch @@ -46,7 +46,7 @@ module God templog.close # send to regular logger - self.send(level, text) + self.send(level, text % []) # send to syslog Syslog.send(SYSLOG_EQUIVALENTS[level], text) diff --git a/test/test_god.rb b/test/test_god.rb index 246bda1..b9c1083 100644 --- a/test/test_god.rb +++ b/test/test_god.rb @@ -520,6 +520,17 @@ class TestGod < Test::Unit::TestCase God.expects(:start).once God.at_exit end + + # pattern_match + + def test_pattern_match + list = %w{ mongrel-3000 mongrel-3001 fuzed fuzed2 apache mysql} + + assert_equal %w{ mongrel-3000 }, God.pattern_match(list, 'm3000') + assert_equal %w{ mongrel-3001 }, God.pattern_match(list, 'm31') + assert_equal %w{ fuzed fuzed2 }, God.pattern_match(list, 'fu') + assert_equal %w{ mysql }, God.pattern_match(list, 'sql') + end end -- 2.11.4.GIT