Class: EventedBluepill::Triggers::Flapping

Inherits:
EventedBluepill::Trigger show all
Defined in:
lib/evented_bluepill/triggers/flapping.rb

Constant Summary collapse

TRIGGER_STATES =
[:starting, :restarting]
PARAMS =
[:times, :within, :retry_in]

Instance Attribute Summary collapse

Attributes inherited from EventedBluepill::Trigger

#logger, #process, #scheduled_events

Instance Method Summary collapse

Methods inherited from EventedBluepill::Trigger

[], #cancel_all_events, #dispatch!, inherited, #remove_timer, #schedule_event

Constructor Details

#initialize(process, options = {}) ⇒ Flapping

Returns a new instance of Flapping.



12
13
14
15
16
17
18
19
20
21
# File 'lib/evented_bluepill/triggers/flapping.rb', line 12

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

Instance Attribute Details

#timelineObject (readonly)

Returns the value of attribute timeline.



10
11
12
# File 'lib/evented_bluepill/triggers/flapping.rb', line 10

def timeline
  @timeline
end

Instance Method Details

#check_flappingObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/evented_bluepill/triggers/flapping.rb', line 35

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

#notify(transition) ⇒ Object



23
24
25
26
27
28
# File 'lib/evented_bluepill/triggers/flapping.rb', line 23

def notify(transition)
  if TRIGGER_STATES.include?(transition.to_name)
    self.timeline << Time.now.to_i
    self.check_flapping
  end
end

#reset!Object



30
31
32
33
# File 'lib/evented_bluepill/triggers/flapping.rb', line 30

def reset!
  @timeline.clear
  super
end