Sha256: 5a188d42bba68f8ba5c50ef2375548052cc576de85890e81380b42d60d4b7acd

Contents?: true

Size: 1.31 KB

Versions: 13

Compression:

Stored size: 1.31 KB

Contents

module Riemann
  module Babbler
    class PluginManager

      include Riemann::Babbler::Logging
      include Riemann::Babbler::Errors

      CHECK_ALIVE_PLUGINS = 10

      def initialize(sender, array_of_klasses)
        @plugins = array_of_klasses
        @sender  = sender
        @mapping = Hash.new
      end

      def run!
        @plugins.map do |plugin|
          unless plugin.new(@sender).send(:run_plugin)
            log :unknown, "Disable plugin: #{plugin}, because it not started by condition: run_plugin"
            next
          end
          @mapping[plugin] = run_thread(plugin)
        end
        loop do
          check_alive
          sleep CHECK_ALIVE_PLUGINS
        end
      end

      private

      def run_thread(plugin)
        Thread.new {
          log :unknown, "Start plugin #{plugin}"
          plugin.new(@sender).run!
          Signal.trap('TERM') do
            shutdown
          end
        }
      end

      def check_alive
        log :debug, "Check alive of threads [#{@mapping.count}]"
        @mapping.each do |plugin, thread|
          next if thread.alive?
          log :error, "Plugin: #{plugin} is #{thread.inspect}, run it..."
          @mapping[plugin] = run_thread(plugin)
        end
      end

      def shutdown
        exit Errors::USER_CALL_SHUTDOWN
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
riemann-babbler-2.8.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.8.0 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.6.0 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.9 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.8 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.7 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.6 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.5 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.4 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.3 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.2 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.5.0 lib/riemann/babbler/plugin_manager.rb