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.



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

def times
  @times
end

#withinObject

Returns the value of attribute within.



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

def within
  @within
end

Instance Method Details

#prepareObject



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

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

#resetObject



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

def reset
  @timeline.clear
end

#testObject



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

def test
  @timeline << Time.now

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

  history = if within
              "[#{@timeline.size}/#{times} within #{(@timeline.last - @timeline.first).to_i}s]"
            else
              "[#{@timeline.size}/#{times}]"
            end

  if consensus && duration
    self.info = "tries exceeded #{history}"
    true
  else
    self.info = "tries within bounds #{history}"
    false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

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