Sha256: b8da1730605575153282145fd7165fd7cf79041177a981ffbcd254902f61faf7

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Bluepill
  HistoryValue = Struct.new(:value, :critical)

  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.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

        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

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

    def fired?
      @history.count { |v| !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

2 entries across 2 versions & 1 rubygems

Version Path
bluepill-0.1.2 lib/bluepill/condition_watch.rb
bluepill-0.1.1 lib/bluepill/condition_watch.rb