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.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bluepill/condition_watch.rb', line 9

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

  @logger = options.delete(:logger)
  @fires  = options.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
  @include_children = options.delete(:include_children) || false

  self.clear_history!

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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/bluepill/condition_watch.rb', line 6

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/bluepill/condition_watch.rb', line 6

def name
  @name
end

Instance Method Details

#clear_history!Object



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

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

#fired?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/bluepill/condition_watch.rb', line 47

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bluepill/condition_watch.rb', line 24

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

    begin
      value = @process_condition.run(pid, @include_children)
    rescue => e
      logger.err(e.backtrace)
      raise e
    end

    @history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check(value))
    logger.info(to_s)

    return @fires if self.fired?
  end
  EMPTY_ARRAY
end

#to_sObject



51
52
53
54
# File 'lib/bluepill/condition_watch.rb', line 51

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