Class: Cognizant::Process::Triggers::Flapping
- Defined in:
- lib/cognizant/process/triggers/flapping.rb
Constant Summary collapse
- TRIGGER_STATES =
[:starting, :restarting]
Instance Attribute Summary collapse
-
#retries ⇒ Object
Returns the value of attribute retries.
-
#retry_after ⇒ Object
Returns the value of attribute retry_after.
-
#timeline ⇒ Object
readonly
Returns the value of attribute timeline.
-
#times ⇒ Object
Returns the value of attribute times.
-
#within ⇒ Object
Returns the value of attribute within.
Instance Method Summary collapse
- #can_retry? ⇒ Boolean
- #check_flapping ⇒ Object
-
#initialize(options = {}) ⇒ Flapping
constructor
A new instance of Flapping.
- #notify(transition) ⇒ Object
- #reset! ⇒ Object
- #within_duration? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Flapping
Returns a new instance of Flapping.
12 13 14 15 16 17 18 19 20 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 12 def initialize( = {}) @times = [:times] || 5 @within = [:within] || 1 @retry_after = [:retry_after] || 5 @retries = [:retries] || 0 @timeline = Util::RotationalArray.new(@times) @num_of_tries = 0 end |
Instance Attribute Details
#retries ⇒ Object
Returns the value of attribute retries.
9 10 11 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 9 def retries @retries end |
#retry_after ⇒ Object
Returns the value of attribute retry_after.
9 10 11 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 9 def retry_after @retry_after end |
#timeline ⇒ Object (readonly)
Returns the value of attribute timeline.
10 11 12 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 10 def timeline @timeline end |
#times ⇒ Object
Returns the value of attribute times.
9 10 11 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 9 def times @times end |
#within ⇒ Object
Returns the value of attribute within.
9 10 11 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 9 def within @within end |
Instance Method Details
#can_retry? ⇒ Boolean
38 39 40 41 42 43 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 38 def can_retry? # retry_after = 0 means do not retry. self.retry_after > 0 and # retries = 0 means always retry. (self.retries == 0 or (self.retries > 0 and @num_of_tries <= self.retries)) end |
#check_flapping ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 45 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. if within_duration? @num_of_tries += 1 Log[self].debug "Flapping detected (##{@num_of_tries}) for #{@delegate.process.name}(pid:#{@delegate.process.cached_pid})." # 0.1 to ensure the state isn't randomly caught in throw :halt below. @delegate.schedule_event(:unmonitor, [0.1, self.retry_after].min) @delegate.schedule_event(:start, self.retry_after) if can_retry? @timeline.clear # This will prevent a transition from happening in the process state_machine. throw :halt end end |
#notify(transition) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 22 def notify(transition) if TRIGGER_STATES.include?(transition.to_name) self.timeline << Time.now.to_i self.check_flapping end end |
#reset! ⇒ Object
29 30 31 32 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 29 def reset! @timeline.clear @num_of_tries = 0 end |
#within_duration? ⇒ Boolean
34 35 36 |
# File 'lib/cognizant/process/triggers/flapping.rb', line 34 def within_duration? (@timeline.last - @timeline.first) <= self.within end |