lib/riemann/babbler/plugin_loader.rb in riemann-babbler-2.0.0pre8 vs lib/riemann/babbler/plugin_loader.rb in riemann-babbler-2.0.0pre9

- old
+ new

@@ -20,14 +20,16 @@ 'runit', 'tw_cli', 'errors_reporter' ].freeze - attr_accessor :sender + attr_accessor :sender, :load_plugin_names_from_config, :delete_from_autostart def initialize(riemann) @sender = riemann + @load_plugin_names_from_config = Array.new + @delete_from_autostart = Array.new end def all_available_plugins default_plugins_dir = File.expand_path('../plugins/', __FILE__) plugins = [] @@ -42,35 +44,74 @@ log :error, "Directory doesn't exists: #{opts.riemann.plugins_directory}" end plugins end - def require_all_plugins - Riemann::Babbler::Plugin.registered_plugins.clear - all_available_plugins.each { |file| require file } - log :debug, "Require plugins: #{Riemann::Babbler::Plugin.registered_plugins}" + def require_parents # load parent opts.plugins.to_hash.each do |plugin_name, plugin_opts| next if plugin_opts.nil? next unless plugin_opts.kind_of?(Hash) if plugin_opts.has_key? :parent cmd = "class #{underscore_to_name plugin_name} < #{underscore_to_name plugin_opts[:parent]}; end;" cmd += "Riemann::Babbler::Plugin.registered_plugins << #{underscore_to_name plugin_name}" - puts "CMD: #{cmd}" + load_plugin_names_from_config << plugin_name eval(cmd) end end end + def require_from_config + # load array new fea + new_opts = opts.to_hash + opts.plugins.to_hash.each do |plugin_name, plugin_opts| + next if plugin_opts.nil? + next if plugin_name == :dirs + next unless plugin_opts.kind_of?(Array) + plugin_opts.each_with_index do |new_plugin_opts, index| + parent_class = underscore_to_name(plugin_name) + new_plugin_name = "#{plugin_name}_#{index}" + new_class = "#{parent_class}_#{index}" + cmd = "class #{new_class} < #{parent_class}; end;" + cmd += "Riemann::Babbler::Plugin.registered_plugins << #{new_class}" + eval(cmd) + new_opts[:plugins][new_plugin_name.to_sym] = new_plugin_opts # set opts + load_plugin_names_from_config << new_plugin_name.to_s + delete_from_autostart << plugin_name.to_s + end + new_opts[:plugins].delete(plugin_name) # delete old if it array + end + opts_reset!(new_opts) + end + + def require_all_plugins! + Riemann::Babbler::Plugin.registered_plugins.clear + all_available_plugins.each { |file| require file } + log :debug, "Require plugins: #{Riemann::Babbler::Plugin.registered_plugins}" + require_parents + require_from_config + end + def run! - plugin_names_to_run = AUTO_START + opts.plugins.to_hash.keys.map {|name| name.to_s} - require_all_plugins + started_plugins = [] + + plugin_names_to_run = (AUTO_START + + opts.plugins.to_hash.keys.map { |name| name.to_s }).uniq + + require_all_plugins! + + plugin_names_to_run = (plugin_names_to_run + + load_plugin_names_from_config.map {|name| name.to_s}).uniq + + plugin_names_to_run = plugin_names_to_run - delete_from_autostart + Riemann::Babbler::Plugin.registered_plugins.each do |klass| if plugin_names_to_run.include? name_to_underscore(klass.to_s) started_plugins << klass end end + plugin_threads = started_plugins.map do |plugin| Thread.new { log :unknown, "Start plugin #{plugin}" plugin.new(sender).run! }