From 4baedb0667185fb899ad1c1f5a7782d1228807a9 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sun, 2 Mar 2014 12:36:37 +0200 Subject: Change dynamic loading If we use assignment method (#method=) we don't get return value, so we never know that manager fails to load. Also we do not want to rescue failures of input/output/source loading, they are catastrophic and should fail whole process, loading of model is not catastrophic. --- lib/oxidized/manager.rb | 22 +++++++++++++--------- lib/oxidized/node.rb | 11 +++++------ lib/oxidized/nodes.rb | 10 +++++----- 3 files changed, 23 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/oxidized/manager.rb b/lib/oxidized/manager.rb index 5065617..9352eaf 100644 --- a/lib/oxidized/manager.rb +++ b/lib/oxidized/manager.rb @@ -6,11 +6,15 @@ module Oxidized class Manager class << self def load dir, file - require File.join dir, file+'.rb' - obj, Oxidized.mgr.loader = Oxidized.mgr.loader, nil - k = obj[:class].new - k.setup if k.respond_to? :setup - { file => obj[:class] } + begin + require File.join dir, file+'.rb' + obj, Oxidized.mgr.loader = Oxidized.mgr.loader, nil + k = obj[:class].new + k.setup if k.respond_to? :setup + { file => obj[:class] } + rescue LoadError + {} + end end end attr_reader :input, :output, :model, :source @@ -21,22 +25,22 @@ module Oxidized @model = {} @source = {} end - def input= method + def add_input method method = Manager.load Config::InputDir, method return false if method.empty? @input.merge! method end - def output= method + def add_output method method = Manager.load Config::OutputDir, method return false if method.empty? @output.merge! method end - def model= _model + def add_model _model _model = Manager.load Config::ModelDir, _model return false if _model.empty? @model.merge! _model end - def source= _source + def add_source _source return nil if @source.key? _source _source = Manager.load Config::SourceDir, _source return false if _source.empty? diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb index 3027ccd..5a8eb36 100644 --- a/lib/oxidized/node.rb +++ b/lib/oxidized/node.rb @@ -109,7 +109,7 @@ module Oxidized inputs = (opt[:input] or CFG.input[:default]) inputs.split(/\s*,\s*/).map do |input| if not Oxidized.mgr.input[input] - Oxidized.mgr.input = input or raise MethodNotFound, "#{input} not found" + Oxidized.mgr.add_input input or raise MethodNotFound, "#{input} not found for node #{ip}" end Oxidized.mgr.input[input] end @@ -118,18 +118,17 @@ module Oxidized def resolve_output opt output = (opt[:output] or CFG.output[:default]) if not Oxidized.mgr.output[output] - Oxidized.mgr.output = output or raise MethodNotFound, "#{output} not found" + Oxidized.mgr.add_output output or raise MethodNotFound, "#{output} not found for node #{ip}" end Oxidized.mgr.output[output] end def resolve_model opt model = (opt[:model] or CFG.model) - mgr = Oxidized.mgr - if not mgr.model[model] - mgr.model = model or raise ModelNotFound, "#{model} not found" + if not Oxidized.mgr.model[model] + Oxidized.mgr.add_model model or raise ModelNotFound, "#{model} not found for node #{ip}" end - mgr.model[model].new + Oxidized.mgr.model[model].new end end diff --git a/lib/oxidized/nodes.rb b/lib/oxidized/nodes.rb index 9a3d3bd..0d9fb47 100644 --- a/lib/oxidized/nodes.rb +++ b/lib/oxidized/nodes.rb @@ -9,14 +9,14 @@ module Oxidized with_lock do new = [] @source = CFG.source[:default] - Oxidized.mgr.source = @source + Oxidized.mgr.add_source @source Oxidized.mgr.source[@source].new.load.each do |node| begin - n = Node.new node - rescue LoadError => err - Log.warn 'load error %s with node %s' % [err.message, node] + _node = Node.new node + new.push _node + rescue ModelNotFound => err + Log.error "node %s raised %s with message %s" % [node, err.class, err.message] end - new.push n if n end replace new end -- cgit v1.2.1