diff options
Diffstat (limited to 'lib/oxidized/hook.rb')
-rw-r--r-- | lib/oxidized/hook.rb | 129 |
1 files changed, 64 insertions, 65 deletions
diff --git a/lib/oxidized/hook.rb b/lib/oxidized/hook.rb index c27f6fd..915299b 100644 --- a/lib/oxidized/hook.rb +++ b/lib/oxidized/hook.rb @@ -1,89 +1,88 @@ module Oxidized -class HookManager - class << self - def from_config cfg - mgr = new - cfg.hooks.each do |name,h_cfg| - h_cfg.events.each do |event| - mgr.register event.to_sym, name, h_cfg.type, h_cfg + class HookManager + class << self + def from_config cfg + mgr = new + cfg.hooks.each do |name, h_cfg| + h_cfg.events.each do |event| + mgr.register event.to_sym, name, h_cfg.type, h_cfg + end end + mgr end - mgr end - end - - # HookContext is passed to each hook. It can contain anything related to the - # event in question. At least it contains the event name - class HookContext < OpenStruct; end - # RegisteredHook is a container for a Hook instance - class RegisteredHook < Struct.new(:name, :hook); end + # HookContext is passed to each hook. It can contain anything related to the + # event in question. At least it contains the event name + class HookContext < OpenStruct; end - Events = [ - :node_success, - :node_fail, - :post_store, - :nodes_done - ] - attr_reader :registered_hooks + # RegisteredHook is a container for a Hook instance + class RegisteredHook < Struct.new(:name, :hook); end - def initialize - @registered_hooks = Hash.new {|h,k| h[k] = []} - end + Events = [ + :node_success, + :node_fail, + :post_store, + :nodes_done + ] + attr_reader :registered_hooks - def register event, name, hook_type, cfg - unless Events.include? event - raise ArgumentError, - "unknown event #{event}, available: #{Events.join ','}" + def initialize + @registered_hooks = Hash.new { |h, k| h[k] = [] } end - Oxidized.mgr.add_hook hook_type - begin - hook = Oxidized.mgr.hook.fetch(hook_type).new - rescue KeyError - raise KeyError, "cannot find hook #{hook_type.inspect}" - end + def register event, name, hook_type, cfg + unless Events.include? event + raise ArgumentError, + "unknown event #{event}, available: #{Events.join ','}" + end - hook.cfg = cfg + Oxidized.mgr.add_hook hook_type + begin + hook = Oxidized.mgr.hook.fetch(hook_type).new + rescue KeyError + raise KeyError, "cannot find hook #{hook_type.inspect}" + end - @registered_hooks[event] << RegisteredHook.new(name, hook) - Oxidized.logger.debug "Hook #{name.inspect} registered #{hook.class} for event #{event.inspect}" - end + hook.cfg = cfg - def handle event, ctx_params={} - ctx = HookContext.new ctx_params - ctx.event = event + @registered_hooks[event] << RegisteredHook.new(name, hook) + Oxidized.logger.debug "Hook #{name.inspect} registered #{hook.class} for event #{event.inspect}" + end - @registered_hooks[event].each do |r_hook| - begin - r_hook.hook.run_hook ctx - rescue => e - Oxidized.logger.error "Hook #{r_hook.name} (#{r_hook.hook}) failed " + - "(#{e.inspect}) for event #{event.inspect}" + def handle event, ctx_params = {} + ctx = HookContext.new ctx_params + ctx.event = event + + @registered_hooks[event].each do |r_hook| + begin + r_hook.hook.run_hook ctx + rescue => e + Oxidized.logger.error "Hook #{r_hook.name} (#{r_hook.hook}) failed " + + "(#{e.inspect}) for event #{event.inspect}" + end end end end -end -# Hook abstract base class -class Hook - attr_reader :cfg + # Hook abstract base class + class Hook + attr_reader :cfg - def initialize - end + def initialize + end - def cfg=(cfg) - @cfg = cfg - validate_cfg! if self.respond_to? :validate_cfg! - end + def cfg=(cfg) + @cfg = cfg + validate_cfg! if self.respond_to? :validate_cfg! + end - def run_hook ctx - raise NotImplementedError - end + def run_hook ctx + raise NotImplementedError + end - def log(msg, level=:info) - Oxidized.logger.send(level, "#{self.class.name}: #{msg}") + def log(msg, level = :info) + Oxidized.logger.send(level, "#{self.class.name}: #{msg}") + end end - -end end |