Class: Oxidized::HookManager

Inherits:
Object
  • Object
show all
Defined in:
lib/oxidized/hook.rb

Defined Under Namespace

Classes: HookContext, RegisteredHook

Constant Summary collapse

Events =
[
  :node_success,
  :node_fail,
  :post_store,
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHookManager

Returns a new instance of HookManager.



29
30
31
# File 'lib/oxidized/hook.rb', line 29

def initialize
  @registered_hooks = Hash.new {|h,k| h[k] = []}
end

Instance Attribute Details

#registered_hooksObject (readonly)

Returns the value of attribute registered_hooks.



27
28
29
# File 'lib/oxidized/hook.rb', line 27

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oxidized/hook.rb', line 52

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
      Log.error "Hook #{r_hook.name} (#{r_hook.hook}) failed " +
                "(#{e.inspect}) for event #{event.inspect}"
    end
  end
end

#register(event, name, hook_type, cfg) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oxidized/hook.rb', line 33

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
  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)
  Log.debug "Hook #{name.inspect} registered #{hook.class} for event #{event.inspect}"
end