libis-workflow 2.0.25 → 2.0.28
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -1
- data/.gitignore +36 -36
- data/.travis.yml +32 -32
- data/Gemfile +4 -4
- data/LICENSE +20 -20
- data/README.md +380 -380
- data/Rakefile +6 -6
- data/lib/libis/exceptions.rb +6 -6
- data/lib/libis/workflow.rb +41 -41
- data/lib/libis/workflow/action.rb +24 -24
- data/lib/libis/workflow/base/dir_item.rb +13 -13
- data/lib/libis/workflow/base/file_item.rb +80 -80
- data/lib/libis/workflow/base/job.rb +83 -83
- data/lib/libis/workflow/base/logging.rb +66 -66
- data/lib/libis/workflow/base/run.rb +97 -95
- data/lib/libis/workflow/base/work_item.rb +173 -173
- data/lib/libis/workflow/base/workflow.rb +149 -149
- data/lib/libis/workflow/config.rb +22 -22
- data/lib/libis/workflow/dir_item.rb +10 -10
- data/lib/libis/workflow/file_item.rb +15 -15
- data/lib/libis/workflow/job.rb +28 -28
- data/lib/libis/workflow/message_registry.rb +30 -30
- data/lib/libis/workflow/run.rb +34 -34
- data/lib/libis/workflow/status.rb +133 -133
- data/lib/libis/workflow/task.rb +318 -316
- data/lib/libis/workflow/task_group.rb +72 -71
- data/lib/libis/workflow/task_runner.rb +34 -34
- data/lib/libis/workflow/version.rb +5 -5
- data/lib/libis/workflow/work_item.rb +37 -37
- data/lib/libis/workflow/worker.rb +42 -42
- data/lib/libis/workflow/workflow.rb +20 -20
- data/libis-workflow.gemspec +38 -38
- data/spec/items.rb +2 -2
- data/spec/items/test_dir_item.rb +13 -13
- data/spec/items/test_file_item.rb +16 -16
- data/spec/items/test_run.rb +8 -8
- data/spec/spec_helper.rb +8 -8
- data/spec/task_spec.rb +15 -15
- data/spec/tasks/camelize_name.rb +12 -12
- data/spec/tasks/checksum_tester.rb +32 -32
- data/spec/tasks/collect_files.rb +47 -47
- data/spec/workflow_spec.rb +154 -154
- metadata +3 -3
@@ -1,30 +1,30 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
|
-
module Libis
|
4
|
-
module Workflow
|
5
|
-
class MessageRegistry
|
6
|
-
include Singleton
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@message_db = {}
|
10
|
-
end
|
11
|
-
|
12
|
-
def register_message(id, message)
|
13
|
-
@message_db[id] = message
|
14
|
-
end
|
15
|
-
|
16
|
-
def get_message(id)
|
17
|
-
@message_db[id]
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.register_message(id, message)
|
21
|
-
self.instance.register_message id, message
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.get_message(id)
|
25
|
-
self.instance.get_message id
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Libis
|
4
|
+
module Workflow
|
5
|
+
class MessageRegistry
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@message_db = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def register_message(id, message)
|
13
|
+
@message_db[id] = message
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_message(id)
|
17
|
+
@message_db[id]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.register_message(id, message)
|
21
|
+
self.instance.register_message id, message
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.get_message(id)
|
25
|
+
self.instance.get_message id
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/libis/workflow/run.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
require 'securerandom'
|
2
|
-
|
3
|
-
require 'libis/workflow/config'
|
4
|
-
require 'libis/workflow/workflow'
|
5
|
-
|
6
|
-
require 'libis/workflow/base/run'
|
7
|
-
require 'libis/workflow/work_item'
|
8
|
-
|
9
|
-
module Libis
|
10
|
-
module Workflow
|
11
|
-
|
12
|
-
class Run < ::Libis::Workflow::WorkItem
|
13
|
-
include ::Libis::Workflow::Base::Run
|
14
|
-
|
15
|
-
attr_accessor :start_date, :job, :id
|
16
|
-
|
17
|
-
def initialize
|
18
|
-
@start_date = Time.now
|
19
|
-
@job = nil
|
20
|
-
@id = SecureRandom.hex(10)
|
21
|
-
super
|
22
|
-
end
|
23
|
-
|
24
|
-
def id
|
25
|
-
nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def logger
|
29
|
-
self.properties[:logger] || (job.logger rescue nil)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
require 'libis/workflow/config'
|
4
|
+
require 'libis/workflow/workflow'
|
5
|
+
|
6
|
+
require 'libis/workflow/base/run'
|
7
|
+
require 'libis/workflow/work_item'
|
8
|
+
|
9
|
+
module Libis
|
10
|
+
module Workflow
|
11
|
+
|
12
|
+
class Run < ::Libis::Workflow::WorkItem
|
13
|
+
include ::Libis::Workflow::Base::Run
|
14
|
+
|
15
|
+
attr_accessor :start_date, :job, :id
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@start_date = Time.now
|
19
|
+
@job = nil
|
20
|
+
@id = SecureRandom.hex(10)
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
def id
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def logger
|
29
|
+
self.properties[:logger] || (job.logger rescue nil)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
35
|
end
|
@@ -1,133 +1,133 @@
|
|
1
|
-
module Libis
|
2
|
-
module Workflow
|
3
|
-
module Status
|
4
|
-
|
5
|
-
STATUS = {
|
6
|
-
NOT_STARTED: 0,
|
7
|
-
STARTED: 1,
|
8
|
-
DONE: 2,
|
9
|
-
ASYNC_WAIT: 3,
|
10
|
-
ASYNC_HALT: 4,
|
11
|
-
FAILED: 5
|
12
|
-
}
|
13
|
-
|
14
|
-
STATUS_TEXT = [
|
15
|
-
'not started',
|
16
|
-
'started',
|
17
|
-
'done',
|
18
|
-
'waiting for running async process',
|
19
|
-
'waiting for halted async process',
|
20
|
-
'failed'
|
21
|
-
]
|
22
|
-
|
23
|
-
# Changes the status of the object. The status changed is logged in the status_log with the current timestamp.
|
24
|
-
#
|
25
|
-
# @param [String] task namepath of the task
|
26
|
-
# @param [Symbol] status status to set
|
27
|
-
def set_status(task, status)
|
28
|
-
task = task.namepath if task.is_a?(Libis::Workflow::Task)
|
29
|
-
log_entry = self.status_entry(task)
|
30
|
-
case status
|
31
|
-
when :STARTED
|
32
|
-
unless status(task) == :ASYNC_WAIT
|
33
|
-
log_entry = self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
|
34
|
-
end
|
35
|
-
else
|
36
|
-
log_entry ||= self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
|
37
|
-
end
|
38
|
-
log_entry['status'] = status
|
39
|
-
log_entry['updated'] = DateTime.now
|
40
|
-
self.save!
|
41
|
-
end
|
42
|
-
|
43
|
-
# Get last known status symbol for a given task
|
44
|
-
#
|
45
|
-
# @param [String] task task name to check item status for
|
46
|
-
# @return [Symbol] the status code
|
47
|
-
def status(task = nil)
|
48
|
-
entry = self.status_entry(task)
|
49
|
-
status_symbol(entry['status']) rescue :NOT_STARTED
|
50
|
-
end
|
51
|
-
|
52
|
-
# Get last known status text for a given task
|
53
|
-
#
|
54
|
-
# @param [String] task task name to check item status for
|
55
|
-
# @return [Symbol] the status code
|
56
|
-
def status_text(task = nil)
|
57
|
-
entry = self.status_entry(task)
|
58
|
-
status_string(entry['status']) rescue STATUS_TEXT.first
|
59
|
-
end
|
60
|
-
|
61
|
-
# Gets the last known status label of the object.
|
62
|
-
#
|
63
|
-
# @param [String] task name of task to get the status for
|
64
|
-
# @return [String] status label ( = task name + status )
|
65
|
-
def status_label(task = nil)
|
66
|
-
entry = self.status_entry(task)
|
67
|
-
"#{entry['task'] rescue nil}#{entry['status'].capitalize rescue nil}"
|
68
|
-
end
|
69
|
-
|
70
|
-
# Check status of the object.
|
71
|
-
#
|
72
|
-
# @param [Symbol] state status to look for
|
73
|
-
# @param [String] task name of task whose status to check
|
74
|
-
# @return [Boolean] true if the object status matches
|
75
|
-
def check_status(state, task = nil)
|
76
|
-
self.status(task) == state
|
77
|
-
end
|
78
|
-
|
79
|
-
# Compare status with current status of the object.
|
80
|
-
#
|
81
|
-
# @param [Symbol] state
|
82
|
-
# @return [Integer] 1, 0 or -1 depnding on which
|
83
|
-
def compare_status(state, task = nil)
|
84
|
-
STATUS[self.status(task)] <=> STATUS[state]
|
85
|
-
end
|
86
|
-
|
87
|
-
# Update the progress of the working task
|
88
|
-
# @param [String] task namepath of the task
|
89
|
-
# @param [Integer] progress progress indicator (as <progress> of <max> or as % if <max> not set). Default: 0
|
90
|
-
# @param [Integer] max max count.
|
91
|
-
def status_progress(task, progress, max = nil)
|
92
|
-
log_entry = self.status_entry(task)
|
93
|
-
log_entry ||= self.add_status_log('task' => task, 'status' => :STARTED, 'created' => DateTime.now)
|
94
|
-
log_entry['progress'] = progress ? progress : (log_entry['progress'] || 0) + 1
|
95
|
-
log_entry['max'] = max if max
|
96
|
-
log_entry['updated'] = DateTime.now
|
97
|
-
self.save!
|
98
|
-
end
|
99
|
-
|
100
|
-
protected
|
101
|
-
|
102
|
-
# Get last known status entry for a given task
|
103
|
-
#
|
104
|
-
# @param [String] task task name to check item status for
|
105
|
-
# @return [Hash] the status entry
|
106
|
-
def status_entry(task = nil)
|
107
|
-
task = task.namepath if task.is_a?(Libis::Workflow::Task)
|
108
|
-
return self.status_log.last if task.blank?
|
109
|
-
self.status_log.select { |entry| entry['task'] == task }.last
|
110
|
-
end
|
111
|
-
|
112
|
-
# Convert String, Symbol or Integer to correct symbol for the status.
|
113
|
-
# If the input value is nil, the fist status entry is returned.
|
114
|
-
#
|
115
|
-
# @param [String|Symbol|Integer] x string, symbol or integer for status code.
|
116
|
-
# @return [Symbol] the corresponding STATUS symbol
|
117
|
-
def status_symbol(x)
|
118
|
-
return STATUS.key(x) if x.is_a?(Integer)
|
119
|
-
return x if STATUS.has_key?(x)
|
120
|
-
x = x.to_s.upcase.to_sym
|
121
|
-
STATUS.has_key?(x) ? x : nil
|
122
|
-
end
|
123
|
-
|
124
|
-
def status_string(x)
|
125
|
-
return STATUS_TEXT[x] if x.is_a?(Integer)
|
126
|
-
return STATUS_TEXT[STATUS[x]] if STATUS.has_key?(x)
|
127
|
-
x = x.to_s.upcase.to_sym
|
128
|
-
STATUS.has_key?(x) ? STATUS_TEXT[STATUS[x]] : 'unknown status'
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
1
|
+
module Libis
|
2
|
+
module Workflow
|
3
|
+
module Status
|
4
|
+
|
5
|
+
STATUS = {
|
6
|
+
NOT_STARTED: 0,
|
7
|
+
STARTED: 1,
|
8
|
+
DONE: 2,
|
9
|
+
ASYNC_WAIT: 3,
|
10
|
+
ASYNC_HALT: 4,
|
11
|
+
FAILED: 5
|
12
|
+
}
|
13
|
+
|
14
|
+
STATUS_TEXT = [
|
15
|
+
'not started',
|
16
|
+
'started',
|
17
|
+
'done',
|
18
|
+
'waiting for running async process',
|
19
|
+
'waiting for halted async process',
|
20
|
+
'failed'
|
21
|
+
]
|
22
|
+
|
23
|
+
# Changes the status of the object. The status changed is logged in the status_log with the current timestamp.
|
24
|
+
#
|
25
|
+
# @param [String] task namepath of the task
|
26
|
+
# @param [Symbol] status status to set
|
27
|
+
def set_status(task, status)
|
28
|
+
task = task.namepath if task.is_a?(Libis::Workflow::Task)
|
29
|
+
log_entry = self.status_entry(task)
|
30
|
+
case status
|
31
|
+
when :STARTED
|
32
|
+
unless status(task) == :ASYNC_WAIT
|
33
|
+
log_entry = self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
log_entry ||= self.add_status_log('task' => task, 'status' => status, 'created' => DateTime.now)
|
37
|
+
end
|
38
|
+
log_entry['status'] = status
|
39
|
+
log_entry['updated'] = DateTime.now
|
40
|
+
self.save!
|
41
|
+
end
|
42
|
+
|
43
|
+
# Get last known status symbol for a given task
|
44
|
+
#
|
45
|
+
# @param [String] task task name to check item status for
|
46
|
+
# @return [Symbol] the status code
|
47
|
+
def status(task = nil)
|
48
|
+
entry = self.status_entry(task)
|
49
|
+
status_symbol(entry['status']) rescue :NOT_STARTED
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get last known status text for a given task
|
53
|
+
#
|
54
|
+
# @param [String] task task name to check item status for
|
55
|
+
# @return [Symbol] the status code
|
56
|
+
def status_text(task = nil)
|
57
|
+
entry = self.status_entry(task)
|
58
|
+
status_string(entry['status']) rescue STATUS_TEXT.first
|
59
|
+
end
|
60
|
+
|
61
|
+
# Gets the last known status label of the object.
|
62
|
+
#
|
63
|
+
# @param [String] task name of task to get the status for
|
64
|
+
# @return [String] status label ( = task name + status )
|
65
|
+
def status_label(task = nil)
|
66
|
+
entry = self.status_entry(task)
|
67
|
+
"#{entry['task'] rescue nil}#{entry['status'].capitalize rescue nil}"
|
68
|
+
end
|
69
|
+
|
70
|
+
# Check status of the object.
|
71
|
+
#
|
72
|
+
# @param [Symbol] state status to look for
|
73
|
+
# @param [String] task name of task whose status to check
|
74
|
+
# @return [Boolean] true if the object status matches
|
75
|
+
def check_status(state, task = nil)
|
76
|
+
self.status(task) == state
|
77
|
+
end
|
78
|
+
|
79
|
+
# Compare status with current status of the object.
|
80
|
+
#
|
81
|
+
# @param [Symbol] state
|
82
|
+
# @return [Integer] 1, 0 or -1 depnding on which
|
83
|
+
def compare_status(state, task = nil)
|
84
|
+
STATUS[self.status(task)] <=> STATUS[state]
|
85
|
+
end
|
86
|
+
|
87
|
+
# Update the progress of the working task
|
88
|
+
# @param [String] task namepath of the task
|
89
|
+
# @param [Integer] progress progress indicator (as <progress> of <max> or as % if <max> not set). Default: 0
|
90
|
+
# @param [Integer] max max count.
|
91
|
+
def status_progress(task, progress, max = nil)
|
92
|
+
log_entry = self.status_entry(task)
|
93
|
+
log_entry ||= self.add_status_log('task' => task, 'status' => :STARTED, 'created' => DateTime.now)
|
94
|
+
log_entry['progress'] = progress ? progress : (log_entry['progress'] || 0) + 1
|
95
|
+
log_entry['max'] = max if max
|
96
|
+
log_entry['updated'] = DateTime.now
|
97
|
+
self.save!
|
98
|
+
end
|
99
|
+
|
100
|
+
protected
|
101
|
+
|
102
|
+
# Get last known status entry for a given task
|
103
|
+
#
|
104
|
+
# @param [String] task task name to check item status for
|
105
|
+
# @return [Hash] the status entry
|
106
|
+
def status_entry(task = nil)
|
107
|
+
task = task.namepath if task.is_a?(Libis::Workflow::Task)
|
108
|
+
return self.status_log.last if task.blank?
|
109
|
+
self.status_log.select { |entry| entry['task'] == task }.last
|
110
|
+
end
|
111
|
+
|
112
|
+
# Convert String, Symbol or Integer to correct symbol for the status.
|
113
|
+
# If the input value is nil, the fist status entry is returned.
|
114
|
+
#
|
115
|
+
# @param [String|Symbol|Integer] x string, symbol or integer for status code.
|
116
|
+
# @return [Symbol] the corresponding STATUS symbol
|
117
|
+
def status_symbol(x)
|
118
|
+
return STATUS.key(x) if x.is_a?(Integer)
|
119
|
+
return x if STATUS.has_key?(x)
|
120
|
+
x = x.to_s.upcase.to_sym
|
121
|
+
STATUS.has_key?(x) ? x : nil
|
122
|
+
end
|
123
|
+
|
124
|
+
def status_string(x)
|
125
|
+
return STATUS_TEXT[x] if x.is_a?(Integer)
|
126
|
+
return STATUS_TEXT[STATUS[x]] if STATUS.has_key?(x)
|
127
|
+
x = x.to_s.upcase.to_sym
|
128
|
+
STATUS.has_key?(x) ? STATUS_TEXT[STATUS[x]] : 'unknown status'
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
data/lib/libis/workflow/task.rb
CHANGED
@@ -1,316 +1,318 @@
|
|
1
|
-
require 'backports/rails/hash'
|
2
|
-
require 'backports/rails/string'
|
3
|
-
|
4
|
-
require 'libis/tools/parameter'
|
5
|
-
require 'libis/tools/extend/hash'
|
6
|
-
require 'libis/tools/logger'
|
7
|
-
|
8
|
-
require 'libis/workflow'
|
9
|
-
|
10
|
-
module Libis
|
11
|
-
module Workflow
|
12
|
-
|
13
|
-
# noinspection RubyTooManyMethodsInspection
|
14
|
-
class Task
|
15
|
-
include ::Libis::Tools::Logger
|
16
|
-
include ::Libis::Tools::ParameterContainer
|
17
|
-
|
18
|
-
attr_accessor :parent, :name, :workitem, :processing_item
|
19
|
-
|
20
|
-
parameter recursive: false, description: 'Run the task on all subitems recursively.'
|
21
|
-
parameter abort_recursion_on_failure: false, description: 'Stop processing items recursively if one item fails.'
|
22
|
-
parameter retry_count: 0, description: 'Number of times to retry the task if waiting for another process.'
|
23
|
-
parameter retry_interval: 10, description: 'Number of seconds to wait between retries.'
|
24
|
-
|
25
|
-
def self.task_classes
|
26
|
-
ObjectSpace.each_object(::Class).select { |klass| klass < self }
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(parent, cfg = {})
|
30
|
-
@subitems_stopper = false
|
31
|
-
@subtasks_stopper = false
|
32
|
-
self.parent = parent
|
33
|
-
configure cfg
|
34
|
-
end
|
35
|
-
|
36
|
-
def <<(task)
|
37
|
-
raise Libis::WorkflowError, "Processing task '#{self.namepath}' is not allowed to have subtasks."
|
38
|
-
end
|
39
|
-
|
40
|
-
# @param [Libis::Workflow::Base::WorkItem] item
|
41
|
-
def run(item)
|
42
|
-
check_item_type ::Libis::Workflow::Base::WorkItem, item
|
43
|
-
self.workitem = item
|
44
|
-
|
45
|
-
case self.action
|
46
|
-
when :retry
|
47
|
-
if item.check_status(:DONE, self.namepath)
|
48
|
-
debug 'Retry: skipping task %s because it has finished successfully.', item, self.namepath
|
49
|
-
return item
|
50
|
-
end
|
51
|
-
when :failed
|
52
|
-
return item
|
53
|
-
else
|
54
|
-
end
|
55
|
-
|
56
|
-
(parameter(:retry_count)+1).times do
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
o
|
113
|
-
o.merge!(opts[self.
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
next
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
self.
|
129
|
-
|
130
|
-
item =
|
131
|
-
|
132
|
-
subject
|
133
|
-
subject ||= item.
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
(cfg['
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
item
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
run_subitems(item) if parameter(:recursive)
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
1
|
+
require 'backports/rails/hash'
|
2
|
+
require 'backports/rails/string'
|
3
|
+
|
4
|
+
require 'libis/tools/parameter'
|
5
|
+
require 'libis/tools/extend/hash'
|
6
|
+
require 'libis/tools/logger'
|
7
|
+
|
8
|
+
require 'libis/workflow'
|
9
|
+
|
10
|
+
module Libis
|
11
|
+
module Workflow
|
12
|
+
|
13
|
+
# noinspection RubyTooManyMethodsInspection
|
14
|
+
class Task
|
15
|
+
include ::Libis::Tools::Logger
|
16
|
+
include ::Libis::Tools::ParameterContainer
|
17
|
+
|
18
|
+
attr_accessor :parent, :name, :workitem, :processing_item
|
19
|
+
|
20
|
+
parameter recursive: false, description: 'Run the task on all subitems recursively.'
|
21
|
+
parameter abort_recursion_on_failure: false, description: 'Stop processing items recursively if one item fails.'
|
22
|
+
parameter retry_count: 0, description: 'Number of times to retry the task if waiting for another process.'
|
23
|
+
parameter retry_interval: 10, description: 'Number of seconds to wait between retries.'
|
24
|
+
|
25
|
+
def self.task_classes
|
26
|
+
ObjectSpace.each_object(::Class).select { |klass| klass < self }
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(parent, cfg = {})
|
30
|
+
@subitems_stopper = false
|
31
|
+
@subtasks_stopper = false
|
32
|
+
self.parent = parent
|
33
|
+
configure cfg
|
34
|
+
end
|
35
|
+
|
36
|
+
def <<(task)
|
37
|
+
raise Libis::WorkflowError, "Processing task '#{self.namepath}' is not allowed to have subtasks."
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param [Libis::Workflow::Base::WorkItem] item
|
41
|
+
def run(item)
|
42
|
+
check_item_type ::Libis::Workflow::Base::WorkItem, item
|
43
|
+
self.workitem = item
|
44
|
+
|
45
|
+
case self.action
|
46
|
+
when :retry
|
47
|
+
if item.check_status(:DONE, self.namepath)
|
48
|
+
debug 'Retry: skipping task %s because it has finished successfully.', item, self.namepath
|
49
|
+
return item
|
50
|
+
end
|
51
|
+
when :failed
|
52
|
+
return item
|
53
|
+
else
|
54
|
+
end
|
55
|
+
|
56
|
+
(parameter(:retry_count)+1).times do
|
57
|
+
|
58
|
+
i = run_item(item)
|
59
|
+
item = i if i.is_a?(Libis::Workflow::WorkItem)
|
60
|
+
|
61
|
+
case item.status(self.namepath)
|
62
|
+
when :DONE
|
63
|
+
self.action = :run
|
64
|
+
return item
|
65
|
+
when :ASYNC_WAIT
|
66
|
+
self.action = :retry
|
67
|
+
when :ASYNC_HALT
|
68
|
+
break
|
69
|
+
when :FAILED
|
70
|
+
break
|
71
|
+
else
|
72
|
+
return item
|
73
|
+
end
|
74
|
+
|
75
|
+
self.action = :retry
|
76
|
+
|
77
|
+
sleep(parameter(:retry_interval))
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
item.get_run.action = :failed
|
82
|
+
|
83
|
+
return item
|
84
|
+
|
85
|
+
rescue WorkflowError => e
|
86
|
+
error e.message, item
|
87
|
+
set_status item, :FAILED
|
88
|
+
|
89
|
+
rescue WorkflowAbort => e
|
90
|
+
set_status item, :FAILED
|
91
|
+
raise e if parent
|
92
|
+
|
93
|
+
rescue ::Exception => e
|
94
|
+
set_status item, :FAILED
|
95
|
+
fatal "Exception occured: #{e.message}", item
|
96
|
+
debug e.backtrace.join("\n")
|
97
|
+
|
98
|
+
ensure
|
99
|
+
item.save!
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def names
|
104
|
+
(self.parent.names rescue Array.new).push(name).compact
|
105
|
+
end
|
106
|
+
|
107
|
+
def namepath;
|
108
|
+
self.names.join('/');
|
109
|
+
end
|
110
|
+
|
111
|
+
def apply_options(opts)
|
112
|
+
o = {}
|
113
|
+
o.merge!(opts[self.class.to_s] || {})
|
114
|
+
o.merge!(opts[self.name] || opts[self.names.join('/')] || {})
|
115
|
+
|
116
|
+
if o and o.is_a? Hash
|
117
|
+
default_values.each do |name, _|
|
118
|
+
next unless o.key?(name.to_s)
|
119
|
+
next if o[name.to_s].nil?
|
120
|
+
paramdef = get_parameter_definition name.to_sym
|
121
|
+
value = paramdef.parse(o[name.to_s])
|
122
|
+
self.parameter(name.to_sym, value)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def message(severity, msg, *args)
|
128
|
+
taskname = self.namepath rescue nil
|
129
|
+
self.set_application(taskname)
|
130
|
+
item = self.workitem rescue nil
|
131
|
+
item = args.shift if args.size > 0 and args[0].is_a?(::Libis::Workflow::Base::WorkItem)
|
132
|
+
subject = item.namepath rescue nil
|
133
|
+
subject ||= item.name rescue nil
|
134
|
+
subject ||= item.to_s rescue nil
|
135
|
+
self.set_subject(subject)
|
136
|
+
super(severity, msg, *args)
|
137
|
+
end
|
138
|
+
|
139
|
+
def logger
|
140
|
+
(self.parent || self.get_run).logger
|
141
|
+
end
|
142
|
+
|
143
|
+
protected
|
144
|
+
|
145
|
+
def configure(cfg)
|
146
|
+
self.name = cfg['name'] || (cfg['class'] || self.class).to_s.split('::').last
|
147
|
+
(cfg['options'] || {}).merge(
|
148
|
+
cfg.reject { |k, _| %w(options name class).include? k }
|
149
|
+
).symbolize_keys.each do |k, v|
|
150
|
+
self.parameter(k, v)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def run_item(item)
|
155
|
+
@item_skipper = false
|
156
|
+
|
157
|
+
return item if item.status(self.namepath) == :DONE
|
158
|
+
|
159
|
+
pre_process(item)
|
160
|
+
|
161
|
+
if @item_skipper
|
162
|
+
run_subitems(item) if parameter(:recursive)
|
163
|
+
else
|
164
|
+
set_status item, :STARTED
|
165
|
+
self.processing_item = item
|
166
|
+
self.process item
|
167
|
+
item = self.processing_item
|
168
|
+
run_subitems(item) if parameter(:recursive)
|
169
|
+
set_status item, :DONE if item.check_status(:STARTED, self.namepath)
|
170
|
+
end
|
171
|
+
|
172
|
+
post_process item
|
173
|
+
|
174
|
+
item
|
175
|
+
end
|
176
|
+
|
177
|
+
def pre_process(_)
|
178
|
+
true
|
179
|
+
# optional implementation
|
180
|
+
end
|
181
|
+
|
182
|
+
def post_process(_)
|
183
|
+
# optional implementation
|
184
|
+
end
|
185
|
+
|
186
|
+
def run_subitems(parent_item)
|
187
|
+
return unless check_processing_subitems
|
188
|
+
|
189
|
+
items = subitems(parent_item)
|
190
|
+
return unless items.size > 0
|
191
|
+
|
192
|
+
status = Hash.new(0)
|
193
|
+
parent_item.status_progress(self.namepath, 0, items.count)
|
194
|
+
items.each_with_index do |item, i|
|
195
|
+
debug 'Processing subitem (%d/%d): %s', parent_item, i+1, items.size, item.to_s
|
196
|
+
new_item = run_item(item)
|
197
|
+
item = new_item if new_item.is_a?(Libis::Workflow::WorkItem)
|
198
|
+
parent_item.status_progress(self.namepath, i+1)
|
199
|
+
item_status = item.status(self.namepath)
|
200
|
+
status[item_status] += 1
|
201
|
+
break if parameter(:abort_recursion_on_failure) && item_status != :DONE
|
202
|
+
end
|
203
|
+
|
204
|
+
debug '%d of %d subitems passed', parent_item, status[:DONE], items.size
|
205
|
+
substatus_check(status, parent_item, 'item')
|
206
|
+
end
|
207
|
+
|
208
|
+
def substatus_check(status, item, task_or_item)
|
209
|
+
item_status = :DONE
|
210
|
+
|
211
|
+
if (waiting = status[:ASYNC_WAIT]) > 0
|
212
|
+
info "waiting for %d sub#{task_or_item}(s) in async process", item, waiting
|
213
|
+
item_status = :ASYNC_WAIT
|
214
|
+
end
|
215
|
+
|
216
|
+
if (halted = status[:ASYNC_HALT]) > 0
|
217
|
+
warn "%d sub#{task_or_item}(s) halted in async process", item, halted
|
218
|
+
item_status = :ASYNC_HALT
|
219
|
+
end
|
220
|
+
|
221
|
+
if (failed = status[:FAILED]) > 0
|
222
|
+
error "%d sub#{task_or_item}(s) failed", item, failed
|
223
|
+
item_status = :FAILED
|
224
|
+
end
|
225
|
+
|
226
|
+
set_status(item, item_status)
|
227
|
+
end
|
228
|
+
|
229
|
+
def capture_cmd(cmd, *opts)
|
230
|
+
out = StringIO.new
|
231
|
+
err = StringIO.new
|
232
|
+
$stdout = out
|
233
|
+
$stderr = err
|
234
|
+
status = system cmd, *opts
|
235
|
+
return [status, out.string, err.string]
|
236
|
+
ensure
|
237
|
+
$stdout = STDOUT
|
238
|
+
$stderr = STDERR
|
239
|
+
end
|
240
|
+
|
241
|
+
def action=(action)
|
242
|
+
self.get_run.action = action
|
243
|
+
end
|
244
|
+
|
245
|
+
def action
|
246
|
+
self.get_run.action
|
247
|
+
end
|
248
|
+
|
249
|
+
def get_run(item = nil)
|
250
|
+
get_root_item(item).get_run
|
251
|
+
end
|
252
|
+
|
253
|
+
def get_root_item(item = nil)
|
254
|
+
(item || self.workitem).get_root
|
255
|
+
end
|
256
|
+
|
257
|
+
def get_work_dir(item = nil)
|
258
|
+
get_root_item(item).work_dir
|
259
|
+
end
|
260
|
+
|
261
|
+
def stop_processing_subitems
|
262
|
+
@subitems_stopper = true if parameter(:recursive)
|
263
|
+
end
|
264
|
+
|
265
|
+
def check_processing_subitems
|
266
|
+
if @subitems_stopper
|
267
|
+
@subitems_stopper = false
|
268
|
+
return false
|
269
|
+
end
|
270
|
+
true
|
271
|
+
end
|
272
|
+
|
273
|
+
def skip_processing_item
|
274
|
+
@item_skipper = true
|
275
|
+
end
|
276
|
+
|
277
|
+
def set_status(item, state)
|
278
|
+
item.set_status self.namepath, state
|
279
|
+
state
|
280
|
+
end
|
281
|
+
|
282
|
+
def check_item_type(klass, item = nil)
|
283
|
+
item ||= self.workitem
|
284
|
+
unless item.is_a? klass.to_s.constantize
|
285
|
+
raise WorkflowError, "Workitem is of wrong type : #{item.class} - expected #{klass.to_s}"
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
def item_type?(klass, item = nil)
|
290
|
+
item ||= self.workitem
|
291
|
+
item.is_a? klass.to_s.constantize
|
292
|
+
end
|
293
|
+
|
294
|
+
private
|
295
|
+
|
296
|
+
def subtasks
|
297
|
+
self.tasks
|
298
|
+
end
|
299
|
+
|
300
|
+
def subitems(item = nil)
|
301
|
+
(item || self.workitem).get_item_list
|
302
|
+
end
|
303
|
+
|
304
|
+
def default_values
|
305
|
+
self.class.default_values
|
306
|
+
end
|
307
|
+
|
308
|
+
def self.default_values
|
309
|
+
parameter_defs.inject({}) do |hash, parameter|
|
310
|
+
hash[parameter.first] = parameter.last[:default]
|
311
|
+
hash
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
end
|
318
|
+
end
|