Class: Oxidized::HookManager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHookManager

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_hooksObject (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