Sha256: bd67799230e68bd45196bddbb52de7fbb1cc2bb85c7827997227e9a306ca575f

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

# -*- encoding: utf-8 -*-

require 'coolio'

module Bluepill
  class ConditionWatch < Coolio::TimerWatcher
    attr_accessor :logger, :name, :process
    attr_reader :every, :process_condition, :fires

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

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

      self.clear_history!

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

      super(@every, true)
    end

    def on_timer
      self.record_value(self.process_condition.run(@process.actual_pid))
      if self.fired?
        @process.dispatch!(self.fires, self.to_s)
        logger.info "#{self.name} dispatched: #{self.fires}"
      end
    end

    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

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evented_bluepill-0.0.47 lib/bluepill/condition_watch.rb