class Dynflow::ExecutionPlan::Hooks::Register

A register holding information about hook classes and events which should trigger the hooks.

@attr_reader hooks [Hash<Class, Set<Symbol>>] a hash mapping hook classes to events which should trigger the hooks

Attributes

hooks[R]

Public Class Methods

new(hooks = {}) click to toggle source
# File lib/dynflow/execution_plan/hooks.rb, line 15
def initialize(hooks = {})
  @hooks = hooks
end

Public Instance Methods

do_not_use(class_name, on: HOOK_KINDS) click to toggle source

Disables a hook from being run on certain events

@param class_name [Class] class of the hook to disable @param on [Symbol, Array<Symbol>] when should the hook be disabled, one of {HOOK_KINDS} @return [void]

# File lib/dynflow/execution_plan/hooks.rb, line 39
def do_not_use(class_name, on: HOOK_KINDS)
  on = Array[on] unless on.kind_of?(Array)
  validate_kinds!(on)
  if hooks[class_name]
    hooks[class_name] -= on
    hooks.delete(class_name) if hooks[class_name].empty?
  end
end
dup() click to toggle source

Performs a deep clone of the hooks register

@return [Register] new deeply cloned register

# File lib/dynflow/execution_plan/hooks.rb, line 51
def dup
  new_hooks = hooks.reduce({}) do |acc, (key, value)|
    acc.update(key => value.dup)
  end
  self.class.new(new_hooks)
end
on(kind) click to toggle source

Returns which hooks should be run on certain event.

@param kind [Symbol] what kind of hook are we looking for @return [Array<Class>] list of hook classes to execute

# File lib/dynflow/execution_plan/hooks.rb, line 82
def on(kind)
  hooks.select { |_key, on| on.include? kind }.keys
end
run(execution_plan, action, kind) click to toggle source

Runs the registered hooks

@param execution_plan [ExecutionPlan] the execution plan which triggered the hooks @param action [Action] the action which triggered the hooks @param kind [Symbol] the kind of hooks to run, one of {HOOK_KINDS}

# File lib/dynflow/execution_plan/hooks.rb, line 63
def run(execution_plan, action, kind)
  hooks = on(kind)
  return if hooks.empty?
  Executors.run_user_code do
    hooks.each do |hook|
      begin
        action.send(hook, execution_plan)
      rescue => e
        execution_plan.logger.error "Failed to run hook '#{hook}' for action '#{action.class}'"
        execution_plan.logger.debug e
      end
    end
  end
end
use(class_name, on: HOOK_KINDS) click to toggle source

Sets a hook to be run on certain events

@param class_name [Class] class of the hook to be run @param on [Symbol, Array<Symbol>] when should the hook be run, one of {HOOK_KINDS} @return [void]

# File lib/dynflow/execution_plan/hooks.rb, line 24
def use(class_name, on: HOOK_KINDS)
  on = Array[on] unless on.kind_of?(Array)
  validate_kinds!(on)
  if hooks[class_name]
    hooks[class_name] += on
  else
    hooks[class_name] = on
  end
end

Private Instance Methods

validate_kinds!(kinds) click to toggle source
# File lib/dynflow/execution_plan/hooks.rb, line 88
def validate_kinds!(kinds)
  kinds.each do |kind|
    raise "Unknown hook kind '#{kind}'" unless HOOK_KINDS.include?(kind)
  end
end