forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.rb
236 lines (196 loc) · 6.68 KB
/
job.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
class Job < ApplicationRecord
include StateMachine
include UuidMixin
include FilterableMixin
belongs_to :miq_task, :dependent => :delete
belongs_to :miq_server
serialize :options
serialize :context
alias_attribute :jobid, :guid
before_destroy :check_active_on_destroy
after_update_commit :update_linked_task
DEFAULT_TIMEOUT = 300
DEFAULT_USERID = 'system'.freeze
def self.create_job(options = {})
ar_options = options.dup.delete_if { |k, _v| !Job.column_names.include?(k.to_s) }
job = new(ar_options)
job.options = options
job.initialize_attributes
job.save
job.create_miq_task(job.attributes_for_task)
$log.info("Job created: #{job.attributes_log}")
job.signal(:initializing)
job
end
def self.current_job_timeout(_timeout_adjustment = 1)
DEFAULT_TIMEOUT
end
delegate :current_job_timeout, :to => :class
def update_linked_task
miq_task&.update!(attributes_for_task)
end
def initialize_attributes
self.name ||= "#{type} created on #{Time.now.utc}"
self.userid ||= DEFAULT_USERID
self.context ||= {}
self.options ||= {}
self.status = "ok"
self.message = "process initiated"
end
def check_active_on_destroy
if is_active?
_log.warn("Job is active, delete not allowed - #{attributes_log}")
throw :abort
end
_log.info("Job deleted: #{attributes_log}")
true
end
def self.update_message(job_guid, message)
job = Job.find_by(:guid => job_guid)
if job
job.update_message(message)
else
_log.warn("jobs.guid: [#{jobid}] not found")
end
end
def update_message(message)
$log.info("JOB([#{guid}] Message update: [#{message}]")
self.message = message
save
return unless is_active?
# Update worker heartbeat
MiqQueue.get_worker(guid).try(:update_heartbeat)
end
def set_status(message, status = "ok")
self.message = message
self.status = status
save
end
def dispatch_start
_log.info("Dispatch Status is 'pending'")
self.dispatch_status = "pending"
save
@storage_dispatcher_process_finish_flag = false
end
def dispatch_finish
return if @storage_dispatcher_process_finish_flag
_log.info("Dispatch Status is 'finished'")
self.dispatch_status = "finished"
save
@storage_dispatcher_process_finish_flag = true
end
def process_cancel(*args)
options = args.first || {}
options[:message] ||= options[:userid] ? "Job canceled by user [#{options[:useid]}] on #{Time.now}" : "Job canceled on #{Time.now}"
options[:status] ||= "ok"
_log.info("job canceling, #{options[:message]}")
signal(:finish, options[:message], options[:status])
end
def process_error(*args)
message, status = args
_log.error(message.to_s)
set_status(message, status)
end
def process_abort(*args)
message, status = args
_log.error("job aborting, #{message}")
set_status(message, status)
signal(:finish, message, status)
end
def process_finished(*args)
message, status = args
_log.info("job finished, #{message}")
set_status(message, status)
dispatch_finish
end
def timeout!
message = "job timed out after #{Time.now - updated_on} seconds of inactivity. Inactivity threshold [#{current_job_timeout} seconds]"
_log.warn("Job: guid: [#{guid}], #{message}, aborting")
attributes = {:args => [message, "error"]}
MiqQueue.create_with(attributes).put_unless_exists(
:class_name => self.class.base_class.name,
:instance_id => id,
:method_name => "signal_abort",
:role => "smartstate",
:zone => MiqServer.my_zone
)
end
def target_entity
target_class.constantize.find_by(:id => target_id) if target_class
end
def self.check_jobs_for_timeout
$log.debug("Checking for timed out jobs")
begin
in_my_region
.where("state != 'finished' and (state != 'waiting_to_start' or dispatch_status = 'active')")
.where("zone is null or zone = ?", MiqServer.my_zone)
.each do |job|
next unless job.updated_on < job.current_job_timeout(job.timeout_adjustment).seconds.ago
# Allow jobs to run longer if the MiqQueue task is still active. (Limited to MiqServer for now.)
# TODO: can we add method_name, queue_name, role, instance_id to the exists?
if job.miq_server_id && MiqQueue.exists?(:state => %w[dequeue ready], :task_id => job.guid, :class_name => "MiqServer")
next
end
job.timeout!
end
rescue Exception
_log.error($!.to_s)
end
end
def timeout_adjustment
timeout_adjustment = 1
target = target_entity
if target.kind_of?(ManageIQ::Providers::Azure::CloudManager::Vm) ||
target.kind_of?(ManageIQ::Providers::Azure::CloudManager::Template)
timeout_adjustment = 4
end
timeout_adjustment
end
def self.check_for_evm_snapshots(job_not_found_delay = 1.hour)
Snapshot.remove_unused_evm_snapshots(job_not_found_delay)
end
def self.guid_active?(job_guid, timestamp, job_not_found_delay)
job = Job.find_by(:guid => job_guid)
# If job was found, return whether it is active
return job.is_active? unless job.nil?
# If Job is NOT found, consider active if timestamp is newer than (now - delay)
timestamp = if timestamp.kind_of?(String)
timestamp.to_time(:utc)
else
timestamp.to_time rescue nil
end
return false if timestamp.nil?
(timestamp >= job_not_found_delay.seconds.ago)
end
def is_active?
!["finished", "waiting_to_start"].include?(state)
end
def self.delete_older(ts, condition)
_log.info("Queuing deletion of jobs older than: #{ts}")
ids = where("updated_on < ?", ts).where(condition).pluck("id")
delete_by_id(ids)
end
def self.delete_by_id(ids)
_log.info("Queuing deletion of jobs with the following ids: #{ids.inspect}")
MiqQueue.submit_job(
:class_name => name,
:method_name => "destroy",
:priority => MiqQueue::HIGH_PRIORITY,
:args => [ids]
)
end
def attributes_for_task
{:status => status.try(:capitalize),
:state => state == "waiting_to_start" ? MiqTask::STATE_QUEUED : state.try(:capitalize),
:name => name,
:message => message,
:userid => userid,
:miq_server_id => miq_server_id,
:context_data => context,
:zone => zone,
:started_on => started_on}
end
def attributes_log
"guid: [#{guid}], userid: [#{self.userid}], name: [#{self.name}], target class: [#{target_class}], target id: [#{target_id}], process type: [#{type}], server id: [#{miq_server_id}], zone: [#{zone}]"
end
end # class Job