Sha256: dfabc609a06531db22647b8c2c207492b288e18219cc3b4c0766e37419a3540f

Contents?: true

Size: 1.46 KB

Versions: 11

Compression:

Stored size: 1.46 KB

Contents

# -*- encoding: utf-8 -*-
module Bluepill
  class HistoryValue < Struct.new(:value, :critical)
  end

  class ConditionWatch
    attr_accessor :logger, :name
    EMPTY_ARRAY = [].freeze # no need to recreate one every tick

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

      self.clear_history!

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

    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 = @process_condition.run(pid, @include_children)
        @history << HistoryValue.new(@process_condition.format_value(value), @process_condition.check(value))
        self.logger.info(self.to_s)

        return @fires if self.fired?
      end
      EMPTY_ARRAY
    end

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

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

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

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
cloud66-bluepill-0.0.64 lib/bluepill/condition_watch.rb
bluepill-0.0.68 lib/bluepill/condition_watch.rb
bluepill-0.0.67 lib/bluepill/condition_watch.rb
bluepill-0.0.66 lib/bluepill/condition_watch.rb
bluepill-0.0.65 lib/bluepill/condition_watch.rb
bluepill-0.0.64 lib/bluepill/condition_watch.rb
cloud66-bluepill-0.0.63 lib/bluepill/condition_watch.rb
bluepill-0.0.63 lib/bluepill/condition_watch.rb
cloud66-bluepill-0.0.62 lib/bluepill/condition_watch.rb
bluepill-0.0.62 lib/bluepill/condition_watch.rb
bluepill-0.0.61 lib/bluepill/condition_watch.rb