Sha256: fee2d64be0a12b398a13464a3e8c6963cbd5f95f5555ce9900d1a840ad98b264

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

# -*- encoding: utf-8 -*-

module EventedBluepill
  module Triggers
    class Flapping < EventedBluepill::Trigger
      TRIGGER_STATES = [:starting, :restarting]
      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 TRIGGER_STATES.include?(transition.to_name)
          self.timeline << Time.now.to_i
          self.check_flapping
        end
      end

      def reset!
        @timeline.clear
        super
      end

      def check_flapping
        # The process has not flapped if we haven't encountered enough incidents
        return unless (@timeline.compact.length == self.times)

        # 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)
          self.dispatch!(:unmonitor)

          @timeline.clear
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
evented_bluepill-0.0.52 lib/evented_bluepill/triggers/flapping.rb
evented_bluepill-0.0.51 lib/evented_bluepill/triggers/flapping.rb
evented_bluepill-0.0.50 lib/evented_bluepill/triggers/flapping.rb