Class: God::Conditions::Tries

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/tries.rb

Instance Attribute Summary collapse

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

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

Instance Attribute Details

#timesObject

Returns the value of attribute times.



5
6
7
# File 'lib/god/conditions/tries.rb', line 5

def times
  @times
end

#withinObject

Returns the value of attribute within.



5
6
7
# File 'lib/god/conditions/tries.rb', line 5

def within
  @within
end

Instance Method Details

#prepareObject



7
8
9
# File 'lib/god/conditions/tries.rb', line 7

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

#resetObject



11
12
13
# File 'lib/god/conditions/tries.rb', line 11

def reset
  @timeline.clear
end

#testObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/god/conditions/tries.rb', line 21

def test
  @timeline << Time.now
  
  concensus = (@timeline.size == self.times)
  duration = self.within.nil? || (@timeline.last - @timeline.first) < self.within
  
  if within
    history = "[#{@timeline.size}/#{self.times} within #{(@timeline.last - @timeline.first).to_i}s]"
  else
    history = "[#{@timeline.size}/#{self.times}]"
  end
  
  if concensus && duration
    self.info = "tries exceeded #{history}"
    return true
  else
    self.info = "tries within bounds #{history}"
    return false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/god/conditions/tries.rb', line 15

def valid?
  valid = true
  valid &= complain("Attribute 'times' must be specified", self) if self.times.nil?
  valid
end