Class: Kuroko2::Workflow::Node

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

Constant Summary collapse

PATH_REGEXP =
%r(\A(?:/|(?:/\d+-[a-z0-9_]+)+)\z)
TASK_REGISTORY =
{
  root:                  Task::Sequence,
  noop:                  Task::Noop,
  sequence:              Task::Sequence,
  auto_skip_error:       Task::AutoSkipError,
  fork:                  Task::Fork,
  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,
}

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:



41
42
43
44
45
46
47
48
49
# File 'lib/kuroko2/workflow/node.rb', line 41

def initialize(type, option = nil)
  @type       = type.to_sym
  @task_klass = TASK_REGISTORY.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.



24
25
26
# File 'lib/kuroko2/workflow/node.rb', line 24

def children
  @children
end

#optionObject (readonly)

Returns the value of attribute option.



24
25
26
# File 'lib/kuroko2/workflow/node.rb', line 24

def option
  @option
end

#parentObject

Returns the value of attribute parent.



25
26
27
# File 'lib/kuroko2/workflow/node.rb', line 25

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



24
25
26
# File 'lib/kuroko2/workflow/node.rb', line 24

def type
  @type
end

Class Method Details

.deregister(key) ⇒ Object



37
38
39
# File 'lib/kuroko2/workflow/node.rb', line 37

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

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



27
28
29
30
31
32
33
34
35
# File 'lib/kuroko2/workflow/node.rb', line 27

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

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

Instance Method Details

#append_child(child) ⇒ Object



51
52
53
54
# File 'lib/kuroko2/workflow/node.rb', line 51

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

#execute(token) ⇒ Object



56
57
58
59
60
61
# File 'lib/kuroko2/workflow/node.rb', line 56

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:



63
64
65
66
67
68
69
70
# File 'lib/kuroko2/workflow/node.rb', line 63

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



72
73
74
75
76
77
78
# File 'lib/kuroko2/workflow/node.rb', line 72

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

#next_siblingObject



80
81
82
83
84
85
86
# File 'lib/kuroko2/workflow/node.rb', line 80

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

#pathObject



88
89
90
91
92
93
94
# File 'lib/kuroko2/workflow/node.rb', line 88

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

#to_script(indent = 0) ⇒ Object



96
97
98
# File 'lib/kuroko2/workflow/node.rb', line 96

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

#validate_allObject



100
101
102
103
104
105
# File 'lib/kuroko2/workflow/node.rb', line 100

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