Sha256: b953c107a01312488da1797ab073e514a44923cc67f1ad092eaa180ed83e8eef

Contents?: true

Size: 1.38 KB

Versions: 23

Compression:

Stored size: 1.38 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

      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)
        @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

23 entries across 23 versions & 5 rubygems

Version Path
bluepill-rwgps-0.0.63 lib/bluepill/condition_watch.rb
bluepill-rwgps-0.0.62 lib/bluepill/condition_watch.rb
bluepill-rwgps-0.0.61 lib/bluepill/condition_watch.rb
bluepill-rwgps-0.0.60 lib/bluepill/condition_watch.rb
kostya-bluepill-0.0.60.3 lib/bluepill/condition_watch.rb
kostya-bluepill-0.0.60.2 lib/bluepill/condition_watch.rb
kostya-bluepill-0.0.60.1 lib/bluepill/condition_watch.rb
skalar-bluepill-0.0.60.1 lib/bluepill/condition_watch.rb
bluepill-0.0.60 lib/bluepill/condition_watch.rb
bluepill-0.0.59 lib/bluepill/condition_watch.rb
bluepill-0.0.58 lib/bluepill/condition_watch.rb
bluepill-0.0.57 lib/bluepill/condition_watch.rb
bluepill-0.0.56 lib/bluepill/condition_watch.rb
bluepill-0.0.55 lib/bluepill/condition_watch.rb
bluepill-0.0.54 lib/bluepill/condition_watch.rb
bluepill-0.0.53 lib/bluepill/condition_watch.rb
bluepill-0.0.52 lib/bluepill/condition_watch.rb
ryansch-bluepill-0.0.53 lib/bluepill/condition_watch.rb
bluepill-0.0.51 lib/bluepill/condition_watch.rb
bluepill-0.0.50 lib/bluepill/condition_watch.rb