Class: Kuroko2::Workflow::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/autoload/kuroko2/workflow/node.rb

Constant Summary collapse

PATH_REGEXP =
%r(\A(?:/|(?:/\d+-[a-z0-9_]+)+)\z)
TASK_REGISTRY =
{
  root:                  Task::Sequence,
  noop:                  Task::Noop,
  sequence:              Task::Sequence,
  auto_skip_error:       Task::AutoSkipError,
  fork:                  Task::Fork,
  parallel_fork:         Task::ParallelFork,
  env:                   Task::Env,
  execute:               Task::Execute,
  queue:                 Task::Queue,
  sub_process:           Task::SubProcess,
  subprocess:            Task::SubProcess,
  timeout:               Task::Timeout,
  expected_time:         Task::ExpectedTime,
  wait:                  Task::Wait,
  sleep:                 Task::Sleep,
  rails_env:             Task::RailsEnv,
  kuroko_runner:         Task::KurokoRunner,
  echo:                  Task::Echo,
  retry:                 Task::Retry,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, option = nil) ⇒ Node

Returns a new instance of Node.

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/autoload/kuroko2/workflow/node.rb', line 44

def initialize(type, option = nil)
  @type       = type.to_sym
  @task_klass = TASK_REGISTRY.fetch(@type, nil)
  @option     = option.try(:strip)
  @parent     = nil
  @children   = []

  raise AssertionError, "`#{@type}` is not registered in task repository." unless @task_klass
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



27
28
29
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27

def children
  @children
end

#optionObject (readonly)

Returns the value of attribute option.



27
28
29
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27

def option
  @option
end

#parentObject

Returns the value of attribute parent.



28
29
30
# File 'lib/autoload/kuroko2/workflow/node.rb', line 28

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27

def type
  @type
end

Class Method Details

.deregister(key) ⇒ Object



40
41
42
# File 'lib/autoload/kuroko2/workflow/node.rb', line 40

def self.deregister(key)
  TASK_REGISTRY.delete(key)
end

.register(key: nil, klass:) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/autoload/kuroko2/workflow/node.rb', line 30

def self.register(key: nil, klass:)
  key ||= klass.to_s.demodulize.underscore.to_sym

  unless TASK_REGISTRY.has_key?(key)
    TASK_REGISTRY.store(key, klass)
  else
    Kuroko2.logger.warn("Unable to add '#{klass}' to task registry. '#{TASK_REGISTRY[key]}' is already registered.")
  end
end

Instance Method Details

#append_child(child) ⇒ Object



54
55
56
57
# File 'lib/autoload/kuroko2/workflow/node.rb', line 54

def append_child(child)
  child.parent = self
  @children << child
end

#execute(token) ⇒ Object



59
60
61
62
63
64
# File 'lib/autoload/kuroko2/workflow/node.rb', line 59

def execute(token)
  Kuroko2.logger.debug { "(token #{token.uuid}) Execute #{@type} with option '#{@option}'." }
  @task_klass.new(self, token).execute.tap do |result|
    Kuroko2.logger.debug("(token #{token.uuid}) Result is '#{result}'.")
  end
end

#find(path) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
# File 'lib/autoload/kuroko2/workflow/node.rb', line 66

def find(path)
  raise AssertionError, "path query('#{path}') is invalid." unless PATH_REGEXP === path

  query = path.split('/')
  query.shift # drop first empty string.

  traverse(query)
end

#next(index = 0) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/autoload/kuroko2/workflow/node.rb', line 75

def next(index = 0)
  if (child = children[index])
    child
  else
    next_sibling
  end
end

#next_siblingObject



83
84
85
86
87
88
89
# File 'lib/autoload/kuroko2/workflow/node.rb', line 83

def next_sibling
  if parent
    parent.next(current_index + 1)
  else
    nil
  end
end

#pathObject



91
92
93
94
95
96
97
# File 'lib/autoload/kuroko2/workflow/node.rb', line 91

def path
  if parent
    parent.path + "/#{current_index}-#{type}"
  else
    ''
  end
end

#to_script(indent = 0) ⇒ Object



99
100
101
# File 'lib/autoload/kuroko2/workflow/node.rb', line 99

def to_script(indent = 0)
  "#{'  ' * indent}#{type}: #{option}\n" + children.map { |child| child.to_script(indent + 1) }.join
end

#validate_allObject



103
104
105
106
107
108
# File 'lib/autoload/kuroko2/workflow/node.rb', line 103

def validate_all
  @task_klass.new(self, nil).validate
  @children.each do |child|
    child.validate_all
  end
end