Sha256: 7491fdbca9c134e02953d02205a0efaee91d0dc80226c7f56b57470d95cbeeac

Contents?: true

Size: 1.25 KB

Versions: 22

Compression:

Stored size: 1.25 KB

Contents

module Riemann
  module Babbler
    class PluginManager

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

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

      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

22 entries across 22 versions & 1 rubygems

Version Path
riemann-babbler-2.1.5 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.4 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.3.2 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.3.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.3 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.2 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.1.0 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.9 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.8 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.7 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.6.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.6 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.5 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.4 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.3.2 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.3.1 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.3 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.2 lib/riemann/babbler/plugin_manager.rb
riemann-babbler-2.0.1 lib/riemann/babbler/plugin_manager.rb