Module: OsCtld::Hook

Extended by:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld.rb,
lib/osctld/hook.rb

Defined Under Namespace

Classes: Base, Manager, Script

Class Method Summary collapse

Class Method Details

.exist?(event_class, hook_name) ⇒ Boolean

Check if a hook with given name exists

Parameters:

  • event_class (Class)
  • hook_name (Symbol)

Returns:

  • (Boolean)


26
27
28
# File 'lib/osctld/hook.rb', line 26

def self.exist?(event_class, hook_name)
  @hooks[event_class].has_key?(hook_name)
end

.hooks(event_class) ⇒ Hash<Symbol, Class>

Parameters:

  • event_class (Class)

Returns:

  • (Hash<Symbol, Class>)


19
20
21
# File 'lib/osctld/hook.rb', line 19

def self.hooks(event_class)
  @hooks[event_class]
end

.register(event_class, hook_name, hook_class) ⇒ Object

Register hook

Parameters:

  • event_class (Class)
  • hook_name (Symbol)
  • hook_class (Class)


11
12
13
14
15
# File 'lib/osctld/hook.rb', line 11

def self.register(event_class, hook_name, hook_class)
  @hooks ||= {}
  @hooks[event_class] ||= {}
  @hooks[event_class][hook_name] = hook_class
end

.run(event_instance, hook_name, **opts) ⇒ Object

Run user-defined script hook

See module Container::Hooks for available hook names and options.

Parameters:

  • event_instance (Class)
  • hook_name (Symbol)

    hook name

  • opts (Hash)

    hook options



37
38
39
# File 'lib/osctld/hook.rb', line 37

def self.run(event_instance, hook_name, **opts)
  Hook::Manager.run(event_instance, @hooks[event_instance.class][hook_name], opts)
end

.watch(hook, hook_path, pid) ⇒ Object

Spawn a thread that will wait for the results of an async hook

Parameters:

  • hook (Hook::Base)
  • hook_path (String)
  • pid (Integer)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osctld/hook.rb', line 45

def self.watch(hook, hook_path, pid)
  Thread.new do
    Process.wait(pid)
    next if $?.exitstatus == 0

    log(
      :warn,
      hook.event_instance,
      "Hook #{hook.class.hook_name} at #{hook_path} exited with #{$?.exitstatus}"
    )
  end
end