Sha256: 01d2602dab5a2ed7113a6172de74e1cdcee8a99be0e3b369aa228c943b02e1ca

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module Bluepill
  module Triggers
    class Flapping < Bluepill::Trigger
      UP_TO_DOWN = [:up, :down] # to avoid recreating this array on every notify
      
      PARAMS = [:times, :within, :retry_in]
      
      attr_accessor *PARAMS
      attr_reader :timeline
      
      def initialize(process, options = {})
        options.reverse_merge!(:times => 5, :within => 1, :retry_in => 5)
        
        options.each_pair do |name, val|
          instance_variable_set("@#{name}", val) if PARAMS.include?(name)
        end
        
        @timeline = Util::RotationalArray.new(@times)
        super
      end
      
      def notify(transition)
        if [transition.from_name, transition.to_name] == UP_TO_DOWN
          self.timeline << Time.now.to_i
          self.check_flapping
        end
      end

      def check_flapping
        num_occurances = (@timeline.nitems == self.times)
        
        # The process has not flapped if we haven't encountered enough incidents
        return unless num_occurances
        
        # Check if the incident happend within the timeframe
        duration = (@timeline.last - @timeline.first) <= self.within
        
        if duration
          self.logger.info "Flapping detected: retrying in #{self.retry_in} seconds"
          
          self.schedule_event(:start, self.retry_in)
          
          # this happens in the process' thread so we don't have to worry about concurrency issues with this event
          self.dispatch!(:stop)
          
          @timeline.clear
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bluepill-0.0.4 lib/bluepill/triggers/flapping.rb
bluepill-0.0.3 lib/bluepill/triggers/flapping.rb