Class: Logical::Naf::Job
- Inherits:
-
Object
show all
- Includes:
- ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper, Naf::TimeHelper
- Defined in:
- app/models/logical/naf/job.rb
Constant Summary
collapse
- COLUMNS =
[:id,
:server,
:pid,
:queued_time,
:title,
:started_at,
:finished_at,
:run_time,
:affinities,
:tags,
:status]
- ATTRIBUTES =
[:title,
:id,
:status,
:server,
:pid,
:queued_time,
:command,
:started_at,
:finished_at,
:run_time,
:exit_status,
:script_type_name,
:log_level,
:request_to_terminate,
:machine_started_on_server_address,
:machine_started_on_server_name,
:application_run_group_name,
:application_run_group_limit,
:application_run_group_restriction_name]
- FILTER_FIELDS =
[:application_type_id,
:application_run_group_restriction_id,
:priority,
:failed_to_start,
:pid,
:exit_status,
:request_to_terminate,
:started_on_machine_id]
- SEARCH_FIELDS =
[:command, :application_run_group_name]
- ORDER =
Mapping of datatable column positions and job attributes
{ '0' => "id",
'2' => "pid",
'3' => "created_at",
'5' => "started_at",
'6' => "finished_at",
'10' => "status" }
Class Method Summary
collapse
Instance Method Summary
collapse
#time_difference, #time_format
Constructor Details
#initialize(naf_job) ⇒ Job
Returns a new instance of Job.
61
62
63
|
# File 'app/models/logical/naf/job.rb', line 61
def initialize(naf_job)
@job = naf_job
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
65
66
67
68
69
70
71
|
# File 'app/models/logical/naf/job.rb', line 65
def method_missing(method_name, *arguments, &block)
if @job.respond_to?(method_name)
@job.send(method_name, *arguments, &block)
else
super
end
end
|
Class Method Details
.find(id) ⇒ Object
235
236
237
238
|
# File 'app/models/logical/naf/job.rb', line 235
def self.find(id)
physical_job = ::Naf::HistoricalJob.find(id)
physical_job ? new(physical_job) : nil
end
|
.get_job_scope(search) ⇒ Object
.search(search) ⇒ Object
Given search, a hash of the search query for jobs on the queue, build up and return the ActiveRecord scope
We eventually build up these results over created_at/1.week partitions.
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
|
# File 'app/models/logical/naf/job.rb', line 128
def self.search(search)
if search[:order] == "status"
conditions = ""
values = {}
values[:limit] = search[:limit].to_i
values[:offset]= search[:offset].to_i*search[:limit].to_i
FILTER_FIELDS.each do |field|
if search[field].present?
conditions << " AND "
case field
when :failed_to_start, :request_to_terminate
values[field.to_sym] = search[field]
conditions << "#{field} = :#{field}"
else
values[field.to_sym] = search[field].to_i
conditions << "#{field} = :#{field}"
end
end
end
SEARCH_FIELDS.each do |field|
if search[field].present?
conditions << " AND "
conditions << "lower(#{field}) ~ :#{field}"
values[field.to_sym] = Regexp.escape(search[field].downcase)
end
end
status = search[:status].blank? ? :all : search[:status]
sql =
case status.to_sym
when :queued
JobStatuses::Running.all(:queued, conditions) + "union all\n" +
JobStatuses::Queued.all(conditions) + "union all\n" +
JobStatuses::Waiting.all(conditions) + "union all\n" +
JobStatuses::FinishedLessMinute.all(conditions) + "union all\n" +
JobStatuses::Terminated.all(conditions)
when :running
JobStatuses::Running.all(conditions) + "union all\n" +
JobStatuses::FinishedLessMinute.all(conditions) + "union all\n" +
JobStatuses::Terminated.all(conditions)
when :waiting
JobStatuses::Waiting.all(conditions)
when :finished
JobStatuses::Finished.all(conditions)
when :errored
JobStatuses::Errored.all(conditions)
else
JobStatuses::Running.all(:queued, conditions) + "union all\n" +
JobStatuses::Queued.all(conditions) + "union all\n" +
JobStatuses::Waiting.all(conditions) + "union all\n" +
JobStatuses::Finished.all(conditions) + "union all\n" +
JobStatuses::Terminated.all(conditions)
end
sql << "LIMIT :limit OFFSET :offset"
jobs = ::Naf::HistoricalJob.find_by_sql([sql, values]).uniq
jobs.map{ |physical_job| new(physical_job) }
else
job_scope = self.get_job_scope(search)
order, direction = search[:order], search[:direction]
job_scope = job_scope.order("#{order} #{direction}").limit(search[:limit]).offset(search[:offset].to_i*search[:limit].to_i)
if search[:status] == 'waiting'
job_scope = job_scope.select{|job| job.prerequisites.select{ |pre| pre.started_at.nil? }.size > 0 }
end
job_scope.map{|physical_job| new(physical_job) }
end
end
|
.total_display_records(search) ⇒ Object
198
199
200
201
202
203
204
205
206
|
# File 'app/models/logical/naf/job.rb', line 198
def self.total_display_records(search)
job_scope = self.get_job_scope(search)
if search[:status] == 'waiting'
job_scope = job_scope.select{ |job| job.prerequisites.select{ |pre| pre.started_at.nil? }.size > 0 }
end
job_scope.count
end
|
Instance Method Details
#affinities ⇒ Object
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'app/models/logical/naf/job.rb', line 317
def affinities
@job.historical_job_affinity_tabs.map do |tab|
if tab.affinity_short_name.present?
if tab.affinity_parameter.present? && tab.affinity_parameter > 0
tab.affinity_short_name + "(#{tab.affinity_parameter})"
else
tab.affinity_short_name
end
else
tab.affinity_classification_name + '_' + tab.affinity_name
end
end.join(", \n")
end
|
#application_run_group_name ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'app/models/logical/naf/job.rb', line 103
def application_run_group_name
if @job.application_run_group_name.blank?
"not set"
elsif @job.application_run_group_name == @job.command
"command"
else
@job.application_run_group_name
end
end
|
#application_url ⇒ Object
240
241
242
243
244
245
246
|
# File 'app/models/logical/naf/job.rb', line 240
def application_url
if application = @job.application
return Rails.application.routes.url_helpers.application_path(application)
else
return nil
end
end
|
#finished_at ⇒ Object
309
310
311
312
313
314
315
|
# File 'app/models/logical/naf/job.rb', line 309
def finished_at
if value = @job.finished_at
"#{time_ago_in_words(value, true)} ago, #{value.localtime.strftime("%Y-%m-%d %r")}"
else
""
end
end
|
#has_started? ⇒ Boolean
285
286
287
|
# File 'app/models/logical/naf/job.rb', line 285
def has_started?
@job.started_at.present?
end
|
#queued_time ⇒ Object
289
290
291
|
# File 'app/models/logical/naf/job.rb', line 289
def queued_time
created_at.localtime.strftime("%Y-%m-%d %r")
end
|
#run_time ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'app/models/logical/naf/job.rb', line 293
def run_time
if @job.started_at.present?
if @job.finished_at.present?
if @job.started_at > @job.finished_at
time_difference(@job.started_at - @job.finished_at)[1..-1]
else
time_difference(@job.finished_at - @job.started_at)[1..-1]
end
else
time_difference(Time.zone.now - @job.started_at)[1..-1]
end
else
""
end
end
|
#runner ⇒ Object
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'app/models/logical/naf/job.rb', line 341
def runner
if @job.machine_runner_invocation.present?
machine = @job.machine_runner_invocation.machine_runner.machine
if machine.present?
if machine.server_name.present?
machine.server_name.to_s
else
if Rails.env == 'development'
"localhost:#{Rails::Server.new.options[:Port]}"
else
machine.server_address
end
end
end
else
''
end
end
|
#server ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'app/models/logical/naf/job.rb', line 113
def server
if started_on_machine
name = started_on_machine.short_name_if_it_exist
if name.blank?
started_on_machine.server_address
else
name
end
end
end
|
#started_at ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'app/models/logical/naf/job.rb', line 268
def started_at
if @job.started_at.present?
value = Time.zone.now - @job.started_at
if value < 60
"#{value.to_i} seconds ago, #{@job.started_at.localtime.strftime("%Y-%m-%d %r")}"
elsif value < 172_800
time_difference(value, @job.started_at)
elsif value >= 172_800
"#{time_ago_in_words(@job.started_at, true)} ago, #{@job.started_at.localtime.strftime("%Y-%m-%d %r")}"
else
""
end
else
""
end
end
|
#status ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/models/logical/naf/job.rb', line 73
def status
if @job.request_to_terminate && @job.finished_at.nil?
"Terminating"
elsif @job.request_to_terminate && @job.finished_at.present?
"Terminated"
elsif @job.started_at and (not @job.finished_at)
"Running"
elsif (not @job.started_at) and (not @job.finished_at) and @job.failed_to_start
"Failed to Start"
elsif @job.exit_status and @job.exit_status > 0
"Error #{@job.exit_status}"
elsif @job.started_at and @job.finished_at
"Finished"
elsif @job.termination_signal
"Signaled #{@job.termination_signal}"
elsif @job.prerequisites.select { |pre| pre.started_at.nil? }.size > 0
"Waiting"
else
"Queued"
end
end
|
331
332
333
334
335
336
337
338
339
|
# File 'app/models/logical/naf/job.rb', line 331
def tags
if @job.running_job.try(:tags).present?
job_tags = @job.running_job.tags.gsub(/[{}]/,'').split(',')
(job_tags.select { |elem| !['$', '_'].include?elem[0] }).join(', ')
else
nil
end
end
|
#title ⇒ Object
95
96
97
98
99
100
101
|
# File 'app/models/logical/naf/job.rb', line 95
def title
if application
application.title
else
command
end
end
|
#to_detailed_hash ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'app/models/logical/naf/job.rb', line 248
def to_detailed_hash
Hash[ ATTRIBUTES.map{ |m|
case m
when :started_at, :finished_at
if value = @job.send(m)
[m, value.localtime.strftime("%Y-%m-%d %r")]
else
[m, '']
end
else
[m, send(m)]
end
}]
end
|
#to_hash ⇒ Object
Format the hash of a job record nicely for the table
264
265
266
|
# File 'app/models/logical/naf/job.rb', line 264
def to_hash
Hash[ COLUMNS.map{ |m| [m, send(m)] } ]
end
|