Class: God::Conditions::Flapping

Inherits:
TriggerCondition show all
Defined in:
lib/god/conditions/flapping.rb

Overview

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.

Parameters

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

Instance Attribute Summary collapse

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from TriggerCondition

#deregister, #register, #trigger

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, #complain, complain, #friendly_name, #reset

Constructor Details

#initializeFlapping

Returns a new instance of Flapping.



42
43
44
45
# File 'lib/god/conditions/flapping.rb', line 42

def initialize
  super
  self.info = 'process is flapping'
end

Instance Attribute Details

#from_stateObject

Returns the value of attribute from_state.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def from_state
  @from_state
end

#retry_inObject

Returns the value of attribute retry_in.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def retry_in
  @retry_in
end

#retry_timesObject

Returns the value of attribute retry_times.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def retry_times
  @retry_times
end

#retry_withinObject

Returns the value of attribute retry_within.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def retry_within
  @retry_within
end

#timesObject

Returns the value of attribute times.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def times
  @times
end

#to_stateObject

Returns the value of attribute to_state.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def to_state
  @to_state
end

#withinObject

Returns the value of attribute within.



34
35
36
# File 'lib/god/conditions/flapping.rb', line 34

def within
  @within
end

Instance Method Details

#prepareObject



47
48
49
50
# File 'lib/god/conditions/flapping.rb', line 47

def prepare
  @timeline = Timeline.new(times)
  @retry_timeline = Timeline.new(retry_times)
end

#process(event, payload) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/god/conditions/flapping.rb', line 60

def process(event, payload)
  return if event != :state_change

  event_from_state, event_to_state = *payload

  from_state_match = !from_state || Array(from_state).include?(event_from_state)
  to_state_match = !to_state || Array(to_state).include?(event_to_state)

  if from_state_match && to_state_match
    @timeline << Time.now

    consensus = (@timeline.size == times)
    duration = (@timeline.last - @timeline.first) < within

    if consensus && duration
      @timeline.clear
      trigger
      retry_mechanism
    end
  end
rescue => e
  puts e.message
  puts e.backtrace.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/god/conditions/flapping.rb', line 52

def valid?
  valid = true
  valid &= complain("Attribute 'times' must be specified", self) if times.nil?
  valid &= complain("Attribute 'within' must be specified", self) if within.nil?
  valid &= complain("Attributes 'from_state', 'to_state', or both must be specified", self) if from_state.nil? && to_state.nil?
  valid
end