-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.rb
83 lines (71 loc) · 1.93 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
module Stepper
class Process
require 'yaml'
require_relative 'utils'
attr_accessor :name,
:description,
:tasks,
:config,
:config_file_path
def initialize(config_file_path)
@config_file_path = config_file_path
@config = YAML.load_file(config_file_path)
# Get basic info
@name = @config['name']
@description = @config['description']
greet
# Create tasks
# TODO: Bulk could probably be moved to the base Task class
puts 'Deserialising tasks...'
@tasks = []
@config['tasks'].each_with_index do |t, i|
task_details = t.values.first
task_class = Utils.get_class_by_name(
'Stepper',
task_details['type']
)
task = task_class.new(
name: t.keys.first,
step: i + 1,
input_params_hash: task_details['input']
)
parent_task_name = task_details['parent_task_name']
task.parent = get_task_by_name(parent_task_name) unless parent_task_name.nil?
task.process = self
@tasks << task
end
# TODO: Ensure th parents are executed before the child
puts "Tasks loaded."
end
def greet
Utils.write_h1 @name
puts "Description: #{@description}"
end
def run
puts "Running process..."
@tasks.each do |task|
task.perform
task.finish
end
summarise
puts 'Done!'
end
def summarise
require 'text-table'
Utils.write_h2 'Summary'
puts "Completed #{@tasks.size} tasks."
rows = []
@tasks.each do |t|
rows << [t.name, "#{'%.6f' % t.duration}"]
end
table = Text::Table.new(
:head => ['Task', 'Duration'],
:rows => rows
)
puts table
end
def get_task_by_name(name)
@tasks.select { |t| t.name.to_sym == name.to_sym }.first
end
end
end