Class: Dynflow::ExecutionPlan::Hooks::Register

Inherits:
Object
  • Object
show all
Defined in:
lib/dynflow/execution_plan/hooks.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hooks = {}) ⇒ Register

Returns a new instance of Register.



15
16
17
# File 'lib/dynflow/execution_plan/hooks.rb', line 15

def initialize(hooks = {})
  @hooks = hooks
end

Instance Attribute Details

#hooksHash<Class, Set<Symbol>> (readonly)

a hash mapping hook classes to events which should trigger the hooks

Returns:

  • (Hash<Class, Set<Symbol>>)

    the current value of hooks



12
13
14
# File 'lib/dynflow/execution_plan/hooks.rb', line 12

def hooks
  @hooks
end

Instance Method Details

#do_not_use(class_name, on: HOOK_KINDS) ⇒ void

This method returns an undefined value.

Disables a hook from being run on certain events

Parameters:

  • class_name (Class)

    class of the hook to disable

  • on (Symbol, Array<Symbol>) (defaults to: HOOK_KINDS)

    when should the hook be disabled, one of HOOK_KINDS



39
40
41
42
43
44
45
46
# 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

#dupRegister

Performs a deep clone of the hooks register

Returns:

  • (Register)

    new deeply cloned register



51
52
53
54
55
56
# 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) ⇒ Array<Class>

Returns which hooks should be run on certain event.

Parameters:

  • kind (Symbol)

    what kind of hook are we looking for

Returns:

  • (Array<Class>)

    list of hook classes to execute



82
83
84
# 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) ⇒ Object

Runs the registered hooks

Parameters:

  • execution_plan (ExecutionPlan)

    the execution plan which triggered the hooks

  • action (Action)

    the action which triggered the hooks

  • kind (Symbol)

    the kind of hooks to run, one of HOOK_KINDS



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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.error e
      end
    end
  end
end

#use(class_name, on: ExecutionPlan.states) ⇒ void

This method returns an undefined value.

Sets a hook to be run on certain events

Parameters:

  • class_name (Class)

    class of the hook to be run

  • on (Symbol, Array<Symbol>) (defaults to: ExecutionPlan.states)

    when should the hook be run, one of HOOK_KINDS



24
25
26
27
28
29
30
31
32
# File 'lib/dynflow/execution_plan/hooks.rb', line 24

def use(class_name, on: ExecutionPlan.states)
  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