Class: Cognizant::Process::ConditionDelegate

Inherits:
Object
  • Object
show all
Defined in:
lib/cognizant/process/condition_delegate.rb

Defined Under Namespace

Classes: HistoryValue

Constant Summary collapse

EMPTY_ARRAY =

No need to recreate one every tick.

[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ ConditionDelegate

Returns a new instance of ConditionDelegate.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cognizant/process/condition_delegate.rb', line 14

def initialize(name, options = {}, &block)
  @name = name
  @every = options.delete(:every).to_i

  @times = options.delete(:times) || 1
  @times = [@times, @times] unless @times.is_a?(Array) # handles :times => 5
  @times.map(&:to_i)

  @do = options.has_key?(:do) ? [options.delete(:do)] : [:restart]
  @do = [block] if block

  clear_history!

  @condition = Cognizant::Process::Conditions[@name].new(options)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/cognizant/process/condition_delegate.rb', line 12

def name
  @name
end

Instance Method Details

#clear_history!Object



43
44
45
# File 'lib/cognizant/process/condition_delegate.rb', line 43

def clear_history!
  @history = Util::RotationalArray.new(@times.last)
end

#failed_check?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cognizant/process/condition_delegate.rb', line 47

def failed_check?
  @history.count { |v| v and v.critical } >= @times.first
end

#run(pid, tick_number = Time.now.to_i) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cognizant/process/condition_delegate.rb', line 30

def run(pid, tick_number = Time.now.to_i)
  if @last_ran_at.nil? || (@last_ran_at + @every) <= tick_number
    @last_ran_at = tick_number

    value = @condition.run(pid)
    @history << HistoryValue.new(@condition.format_value(value), @condition.check(value))
    # puts self.to_s

    return @do if failed_check?
  end
  EMPTY_ARRAY
end

#to_sObject



51
52
53
54
# File 'lib/cognizant/process/condition_delegate.rb', line 51

def to_s
  data = @history.collect { |v| v and "#{v.value}#{'*' unless v.critical}" }.join(", ")
  "#{@name}: [#{data}]\n"
end