Class: Oxidized::HookManager
- Inherits:
-
Object
- Object
- Oxidized::HookManager
- Defined in:
- lib/oxidized/hook.rb
Defined Under Namespace
Classes: HookContext, RegisteredHook
Constant Summary collapse
- EVENTS =
%i[ node_success node_fail post_store nodes_done ].freeze
Instance Attribute Summary collapse
-
#registered_hooks ⇒ Object
readonly
Returns the value of attribute registered_hooks.
Class Method Summary collapse
Instance Method Summary collapse
- #handle(event, ctx_params = {}) ⇒ Object
-
#initialize ⇒ HookManager
constructor
A new instance of HookManager.
- #register(event, name, hook_type, cfg) ⇒ Object
Constructor Details
#initialize ⇒ HookManager
Returns a new instance of HookManager.
30 31 32 |
# File 'lib/oxidized/hook.rb', line 30 def initialize @registered_hooks = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#registered_hooks ⇒ Object (readonly)
Returns the value of attribute registered_hooks.
28 29 30 |
# File 'lib/oxidized/hook.rb', line 28 def registered_hooks @registered_hooks end |
Class Method Details
.from_config(cfg) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/oxidized/hook.rb', line 4 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 |
Instance Method Details
#handle(event, ctx_params = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/oxidized/hook.rb', line 53 def handle(event, ctx_params = {}) ctx = HookContext.new ctx_params ctx.event = event @registered_hooks[event].each do |r_hook| r_hook.hook.run_hook ctx rescue StandardError => e Oxidized.logger.error "Hook #{r_hook.name} (#{r_hook.hook}) failed " \ "(#{e.inspect}) for event #{event.inspect}" end end |
#register(event, name, hook_type, cfg) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/oxidized/hook.rb', line 34 def register(event, name, hook_type, cfg) unless EVENTS.include? event raise ArgumentError, "unknown event #{event}, available: #{EVENTS.join ','}" end Oxidized.mgr.add_hook(hook_type) || raise("cannot load hook '#{hook_type}', not found") begin hook = Oxidized.mgr.hook.fetch(hook_type).new rescue KeyError raise KeyError, "cannot find hook #{hook_type.inspect}" end hook.cfg = cfg @registered_hooks[event] << RegisteredHook.new(name, hook) Oxidized.logger.debug "Hook #{name.inspect} registered #{hook.class} for event #{event.inspect}" end |