Sha256: e201b8207bf5e4f50828d8aa2c3f99d697578710383095a6a179fd372fc48d9d

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

module Kurchatov
  class Monitor

    class Task
      include Kurchatov::Mixin::Event
      attr_accessor :thread, :instance
      attr_reader :count_errors, :last_error, :last_error_at

      def initialize(plugin)
        @plugin = plugin
        @thread = Thread.new { @plugin.start }
        @count_errors = 0
        @last_error = nil
        @last_error_at = nil
      end

      def name
        @plugin.name
      end

      def config
        @plugin.plugin_config
      end

      def died?
        return false if @thread.alive?
        # thread died, join and extract error
        begin
          @thread.join # call error
        rescue => e
          desc = "Plugin '#{@plugin.name}' died. #{e.class}: #{e}\n." +
            "Trace:  #{e.backtrace.join("\n")}"
          @count_errors += 1
          @last_error = desc
          @last_error_at = Time.now
          Log.error(desc)
          unless @plugin.ignore_errors
            event(:service => 'riemann client errors', :desc => desc, :state => 'critical')
          end
        end
        @thread = Thread.new { @plugin.start }
        true
      end

    end

    attr_accessor :tasks
    CHECK_ALIVE_TIMEOUT = 5

    def initialize(stop = false)
      @stop_on_error = stop
      @tasks = Array.new
    end

    def <<(plugin)
      Log.debug("Add new plugin: #{plugin.inspect}")
      @tasks << Task.new(plugin)
    end

    def start
      loop do
        @tasks.each { |t| exit Config[:ERROR_PLUGIN_REQ] if t.died? && @stop_on_error }
        Log.debug("Check alive plugins [#{@tasks.count}]")
        sleep CHECK_ALIVE_TIMEOUT
      end
    end

    def inspect
      @tasks.map do |t|
        {
          "name" => t.name,
          "config" => t.config,
          "errors" => {"count" => t.count_errors, "last" => t.last_error, "time" => t.last_error_at}
        }
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kurchatov-0.1.0 lib/kurchatov/monitor.rb
kurchatov-0.0.9 lib/kurchatov/monitor.rb
kurchatov-0.0.8.pre.3 lib/kurchatov/monitor.rb
kurchatov-0.0.7.pre.4 lib/kurchatov/monitor.rb
kurchatov-0.0.8.pre.2 lib/kurchatov/monitor.rb
kurchatov-0.0.8.pre.1 lib/kurchatov/monitor.rb
kurchatov-0.0.7 lib/kurchatov/monitor.rb