Class: Bluepill::ConditionWatch

Inherits:
Object
  • Object
show all
Defined in:
lib/bluepill/condition_watch.rb

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 = {}) ⇒ ConditionWatch

Returns a new instance of ConditionWatch.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bluepill/condition_watch.rb', line 6

def initialize(name, options = {})
  @name = name

  @logger = options.delete(:logger)      
  @fires = options.has_key?(:fires) ? Array(options.delete(:fires)) : [:restart]
  @every = options.delete(:every)
  @times = options.delete(:times) || [1,1]
  @times = [@times, @times] unless @times.is_a?(Array) # handles :times => 5

  self.clear_history!
  
  @process_condition = ProcessConditions[@name].new(options)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/bluepill/condition_watch.rb', line 3

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/bluepill/condition_watch.rb', line 3

def name
  @name
end

Instance Method Details

#clear_history!Object



36
37
38
39
40
# File 'lib/bluepill/condition_watch.rb', line 36

def clear_history!
  @last_ran_at = nil
  @history = Array.new(@times[1])
  @history_index = 0
end

#fired?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bluepill/condition_watch.rb', line 42

def fired?
  @history.select {|v| v && !v[1]}.size >= @times[0]
end

#record_value(value) ⇒ Object



29
30
31
32
33
34
# File 'lib/bluepill/condition_watch.rb', line 29

def record_value(value)
  # TODO: record value in ProcessStatistics
  @history[@history_index] = [value, @process_condition.check(value)]
  @history_index = (@history_index + 1) % @history.size
  self.logger.info(self.to_s)
end

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



20
21
22
23
24
25
26
27
# File 'lib/bluepill/condition_watch.rb', line 20

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
    self.record_value(@process_condition.run(pid))
    return @fires if self.fired?
  end
  EMPTY_ARRAY
end

#to_sObject



46
47
48
49
50
51
52
53
# File 'lib/bluepill/condition_watch.rb', line 46

def to_s
  # TODO: this will be out of order because of the way history values are assigned
  # use (@history[(@history_index - 1)..1] + @history[0..(@history_index - 1)]).
  #       collect {|v| "#{v[0]}#{v[1] ? '' : '*'}"}.join(", ")
  # but that's gross so... it's gonna be out of order till we figure out a better way to get it in order
  data = @history.collect {|v| "#{@process_condition.format_value(v[0])}#{v[1] ? '' : '*'}" if v}.compact.join(", ")
  "#{@name}: [#{data}]"
end