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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/genesisframework/tasks.rb', line 52
def self.execute task_name
puts "\n#{task_name}\n================================================="
prompt_timeout = ENV['GENESIS_PROMPT_TIMEOUT'] \
|| Genesis::Framework::Utils.config_cache['task_prompt_timeout'] \
|| 10
if prompt_timeout.to_i > 0
return true unless Genesis::PromptCLI.ask("Would you like to run this task?", prompt_timeout, true)
end
task = Genesis::Framework::Tasks.const_get(task_name)
if task.blocks.nil?
puts "task is empty with nothing to do, skipping..."
return true
end
begin
puts "task is now testing if it needs to be initialized..."
if task.blocks.has_key?(:precondition)
task.blocks[:precondition].each do |description, block|
puts "Testing: %s" % description
unless self.call_block(task.blocks[:precondition], description)
puts "task is being skipped..."
return true
end
end
end
rescue => e
puts "%s task had error on testing if it needs initialization: %s" % [task_name, e.message]
return false
end
begin
puts "task is now initializing..."
self.call_block(task.blocks, :init);
puts "task is now initialized..."
rescue => e
puts "%s task threw error on initialization: %s" % [task_name, e.message]
return false
end
begin
puts "task is now testing if it can run..."
if task.blocks.has_key?(:condition)
task.blocks[:condition].each do |description, block|
puts "Checking: %s" % description
unless self.call_block(task.blocks[:condition], description)
puts "Conditional failed. Task is being skipped."
return true
end
end
end
rescue => e
puts "%s task had error on testing if it needs running: %s" % [task_name, e.message]
return false
end
success = nil
task.options[:retries].each_with_index do |sleep_interval, index|
attempt = index + 1
begin
puts "task is attempting run #%d..." % [attempt]
Timeout::timeout(task.options[:timeout]) do
success = self.call_block(task.blocks, :run)
end
success = true if success.nil?
rescue => e
puts "%s task [run #%d] caused error: %s" % [task_name, attempt, e.message]
success = nil end
break unless success.nil? puts "task is sleeping for %d seconds..." % [sleep_interval]
Kernel.sleep(sleep_interval)
end
success = false if success.nil?
if success
success = self.call_block(task.blocks, :success)
puts "task is successful!"
else
puts 'task failed!!!'
if self.has_block? task.blocks, :rollback
success = self.call_block(task.blocks, :rollback, "rolling back!")
end
end
puts "\n\n"
return success
end
|