Sha256: 01510faa1a6ed275bc2f1e44d7ad70a5e12e647810ec0591c3fc58a5c1b938b5

Contents?: true

Size: 1.56 KB

Versions: 69

Compression:

Stored size: 1.56 KB

Contents

module God
  module Behaviors
    
    class NotifyWhenFlapping < Behavior
      attr_accessor :failures # number of failures 
      attr_accessor :seconds  # number of seconds
      attr_accessor :notifier # class to notify with
      
      def initialize
        super
        @startup_times = []
      end
      
      def valid?
        valid = true
        valid &= complain("Attribute 'failures' must be specified", self) unless self.failures
        valid &= complain("Attribute 'seconds' must be specified", self) unless self.seconds
        valid &= complain("Attribute 'notifier' must be specified", self) unless self.notifier
                
        # Must take one arg or variable args
        unless self.notifier.respond_to?(:notify) and [1,-1].include?(self.notifier.method(:notify).arity)
          valid &= complain("The 'notifier' must have a method 'notify' which takes 1 or variable args", self)
        end
        
        valid
      end
  
      def before_start
        now = Time.now.to_i
        @startup_times << now
        check_for_flapping(now)
      end
      
      def before_restart
        now = Time.now.to_i
        @startup_times << now
        check_for_flapping(now)
      end
      
      private
        
        def check_for_flapping(now)
          @startup_times.select! {|time| time >= now - self.seconds }
          if @startup_times.length >= self.failures
            self.notifier.notify("#{self.watch.name} has called start/restart #{@startup_times.length} times in #{self.seconds} seconds")
          end
        end
    end
  
  end
end

Version data entries

69 entries across 69 versions & 21 rubygems

Version Path
dguettler-god-0.7.13.2 lib/god/behaviors/notify_when_flapping.rb
dguettler-god-0.7.13.3 lib/god/behaviors/notify_when_flapping.rb
dosire-god-0.7.10 lib/god/behaviors/notify_when_flapping.rb
dosire-god-0.7.12 lib/god/behaviors/notify_when_flapping.rb
dosire-god-0.7.9 lib/god/behaviors/notify_when_flapping.rb
dunedain289-god-0.7.12.1 lib/god/behaviors/notify_when_flapping.rb
dustin-god-0.7.7.1 lib/god/behaviors/notify_when_flapping.rb
eric-god-0.7.10 lib/god/behaviors/notify_when_flapping.rb
eric-god-0.7.11 lib/god/behaviors/notify_when_flapping.rb
eric-god-0.7.12 lib/god/behaviors/notify_when_flapping.rb
eric-god-0.7.14 lib/god/behaviors/notify_when_flapping.rb
eric-god-0.7.7 lib/god/behaviors/notify_when_flapping.rb
fotonauts-god-0.7.10 lib/god/behaviors/notify_when_flapping.rb
fotonauts-god-0.7.12 lib/god/behaviors/notify_when_flapping.rb
gohanlonllc-god-0.7.9 lib/god/behaviors/notify_when_flapping.rb
gordoncww-god-0.7.11.1 lib/god/behaviors/notify_when_flapping.rb
jreynolds-god-0.7.13 lib/god/behaviors/notify_when_flapping.rb
jwilkins-god-0.7.9 lib/god/behaviors/notify_when_flapping.rb
mathieuravaux-god-0.7.11 lib/god/behaviors/notify_when_flapping.rb
mojombo-god-0.7.10 lib/god/behaviors/notify_when_flapping.rb