Class: God::Conditions::GrowingQueue

Inherits:
PollCondition
  • Object
show all
Defined in:
lib/growing_queue_condition/base.rb

Overview

Condition Symbol :growing_queue Type: Poll

Trigger when a queue is growing

Parameters Required

+obj+ is an instance of a queue interface class

Optional

+method+ is the method to call, it defaults to queue_size

Examples

Trigger if the queue has grown 3 out of the last 5 checks

restart.condition(:growing_queue) do |c|

c.times     = [3,5]
c.interval  = 30.seconds
c.obj       = MyClass.new

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGrowingQueue

Returns a new instance of GrowingQueue.



27
28
29
30
31
# File 'lib/growing_queue_condition/base.rb', line 27

def initialize
  super
  self.times ||= [2, 3]
  self.meth ||= :queue_size
end

Instance Attribute Details

#methObject

Returns the value of attribute meth.



24
25
26
# File 'lib/growing_queue_condition/base.rb', line 24

def meth
  @meth
end

#objObject

Returns the value of attribute obj.



24
25
26
# File 'lib/growing_queue_condition/base.rb', line 24

def obj
  @obj
end

#timesObject

Returns the value of attribute times.



24
25
26
# File 'lib/growing_queue_condition/base.rb', line 24

def times
  @times
end

Instance Method Details

#prepareObject



34
35
36
37
38
39
40
# File 'lib/growing_queue_condition/base.rb', line 34

def prepare
  if self.times.kind_of? Integer
    self.times = [self.times, self.times]
  end

  @timeline = Timeline.new(self.times[1])
end

#resetObject



43
44
45
# File 'lib/growing_queue_condition/base.rb', line 43

def reset
  @timeline.clear
end

#testObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/growing_queue_condition/base.rb', line 55

def test
  failing = false
  count = obj.call(meth)
  num_of_fails = times[0]
  num_of_tests = times[1]

  @timeline.push count.to_i

  history = "[#{@timeline.join(', ')}]"

  if @timeline.length < num_of_tests
    self.info = 'not enough info'
    return false
  end

  ary = @timeline.dup.flatten
  fails = []

  (num_of_fails == num_of_tests ? num_of_fails-1 : num_of_fails).times do
    if ary[-1] == 0
      fails << false
    else
      fails << (ary[-1] >= ary[-2])
    end
    ary.shift
  end

  bad, good = fails.partition { |f| TrueClass === f }
  failing = bad.length >= num_of_fails

  if failing
    self.info = "count increasing: (#{bad.length} fails #{history}"
    return true
  else
    self.info = "count ok: #{history}"
    return false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/growing_queue_condition/base.rb', line 48

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