Class: Naf::HistoricalJob

Inherits:
Partitioned::ById
  • Object
show all
Includes:
PgAdvisoryLocker
Defined in:
app/models/naf/historical_job.rb

Overview

Things you should know about jobs new jobs older than 1.week will not be searched in the queue. You should not run programs for more than a 1.week (instead, have them exit and restarted periodically)

Defined Under Namespace

Classes: JobPrerequisiteLoop

Constant Summary collapse

JOB_STALE_TIME =
1.week
SYSTEM_TAGS =
{
  startup: '$startup',
  pre_work: '$pre-work',
  work: '$work',
  cleanup: '$cleanup'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.application_last_queuedObject



176
177
178
179
180
# File 'app/models/naf/historical_job.rb', line 176

def self.application_last_queued
  where("application_id IS NOT NULL").
    group("application_id").
    select("application_id, MAX(id) AS id, MAX(created_at) AS created_at")
end

.application_last_runsObject



169
170
171
172
173
174
# File 'app/models/naf/historical_job.rb', line 169

def self.application_last_runs
  where("application_id IS NOT NULL").
    group("application_id").
    select("application_id, MAX(finished_at) AS finished_at").
    reject{ |job| job.finished_at.nil? }
end

.canceledObject



165
166
167
# File 'app/models/naf/historical_job.rb', line 165

def self.canceled
  where(request_to_terminate: true)
end

.connectionObject


*** Class Methods *** ++++++++++++++++++++++



145
146
147
# File 'app/models/naf/historical_job.rb', line 145

def self.connection
  ::Naf::NafBase.connection
end

.erroredObject



201
202
203
# File 'app/models/naf/historical_job.rb', line 201

def self.errored
  where("finished_at IS NOT NULL AND exit_status > 0 OR request_to_terminate = true")
end

.finishedObject



182
183
184
# File 'app/models/naf/historical_job.rb', line 182

def self.finished
  where("finished_at IS NOT NULL OR request_to_terminate = true")
end

.full_table_name_prefixObject



149
150
151
# File 'app/models/naf/historical_job.rb', line 149

def self.full_table_name_prefix
  ::Naf::NafBase.full_table_name_prefix
end

.lock_for_job_queue(&block) ⇒ Object



205
206
207
# File 'app/models/naf/historical_job.rb', line 205

def self.lock_for_job_queue(&block)
  lock_record(0, &block)
end

.partition_num_lead_buffersObject



157
158
159
# File 'app/models/naf/historical_job.rb', line 157

def self.partition_num_lead_buffers
  10
end

.partition_table_sizeObject



153
154
155
# File 'app/models/naf/historical_job.rb', line 153

def self.partition_table_size
  100000
end

.queued_between(start_time, end_time) ⇒ Object



161
162
163
# File 'app/models/naf/historical_job.rb', line 161

def self.queued_between(start_time, end_time)
  where(["created_at >= ? AND created_at <= ?", start_time, end_time])
end

.queued_statusObject



186
187
188
189
190
# File 'app/models/naf/historical_job.rb', line 186

def self.queued_status
  where("(started_at IS NULL AND request_to_terminate = false) OR
         (finished_at > '#{Time.zone.now - 1.minute}') OR
         (started_at IS NOT NULL AND finished_at IS NULL AND request_to_terminate = false)")
end

.queued_with_waitingObject



197
198
199
# File 'app/models/naf/historical_job.rb', line 197

def self.queued_with_waiting
  where("(started_at IS NULL AND request_to_terminate = false)")
end

.running_statusObject



192
193
194
195
# File 'app/models/naf/historical_job.rb', line 192

def self.running_status
  where("(started_at IS NOT NULL AND finished_at IS NULL AND request_to_terminate = false) OR
         (finished_at > '#{Time.zone.now - 1.minute}')")
end

Instance Method Details

#add_tags(tags_to_add) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/models/naf/historical_job.rb', line 305

def add_tags(tags_to_add)
  tags_array = nil
  if self.tags.present?
    tags_array = self.tags.gsub(/[{}]/,'').split(',')
    new_tags = '{' + (tags_array | tags_to_add).join(',') + '}'
  else
    new_tags = '{' + tags_to_add.join(',') + '}'
  end

  self.tags = new_tags
  self.save!
end

#affinity_idsObject



272
273
274
# File 'app/models/naf/historical_job.rb', line 272

def affinity_ids
  historical_job_affinity_tabs.map{ |jat| jat.affinity_id }
end

#historical_job_affinity_tabsObject



262
263
264
265
266
# File 'app/models/naf/historical_job.rb', line 262

def historical_job_affinity_tabs
  ::Naf::HistoricalJobAffinityTab.
    from_partition(id).
    where(historical_job_id: id)
end

#historical_job_prerequisitesObject



276
277
278
279
280
# File 'app/models/naf/historical_job.rb', line 276

def historical_job_prerequisites
  ::Naf::HistoricalJobPrerequisite.
    from_partition(id).
    where(historical_job_id: id)
end

#job_affinitiesObject



268
269
270
# File 'app/models/naf/historical_job.rb', line 268

def job_affinities
  historical_job_affinity_tabs.map{ |jat| jat.affinity }
end

#machine_started_on_server_addressObject



258
259
260
# File 'app/models/naf/historical_job.rb', line 258

def machine_started_on_server_address
  started_on_machine.try(:server_address)
end

#machine_started_on_server_nameObject



254
255
256
# File 'app/models/naf/historical_job.rb', line 254

def machine_started_on_server_name
  started_on_machine.try(:server_name)
end

#prerequisitesObject



282
283
284
285
286
287
# File 'app/models/naf/historical_job.rb', line 282

def prerequisites
  historical_job_prerequisites.
    map{ |hjp| ::Naf::HistoricalJob.from_partition(hjp.prerequisite_historical_job_id).
    find_by_id(hjp.prerequisite_historical_job_id) }.
    reject{ |j| j.nil? }
end

#remove_all_tagsObject



328
329
330
331
# File 'app/models/naf/historical_job.rb', line 328

def remove_all_tags
  self.tags = '{}'
  self.save!
end

#remove_tags(tags_to_remove) ⇒ Object



318
319
320
321
322
323
324
325
326
# File 'app/models/naf/historical_job.rb', line 318

def remove_tags(tags_to_remove)
  if self.tags.present?
    tags_array = self.tags.gsub(/[{}]/,'').split(',')
    new_tags = '{' + (tags_array - tags_to_remove).join(',') + '}'

    self.tags = new_tags
    self.save!
  end
end

#spawnObject



301
302
303
# File 'app/models/naf/historical_job.rb', line 301

def spawn
  application_type.spawn(self)
end

#titleObject



250
251
252
# File 'app/models/naf/historical_job.rb', line 250

def title
  application.try(:title)
end

#to_sObject


*** Instance Methods *** +++++++++++++++++++++++++



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
# File 'app/models/naf/historical_job.rb', line 213

def to_s
  components = []

  if started_at.nil?
    components << "QUEUED"
  else
    if finished_at.nil?
      if pid
        extras = []
        extras << pid.to_s
        extras << 'RequestedToTerminate' if request_to_terminate
        components << "RUNNING:#{extras.join(':')}"
      else
        components << "SPAWNING"
      end
    else
      extras = []
      extras << 'RequestedToTerminate' if request_to_terminate
      extras << "FailedToStart" if failed_to_start
      extras << "SIG#{termination_signal}" if termination_signal
      if exit_status && exit_status != 0
        extras << "STATUS=#{exit_status}"
      end
      if extras.length
        extras_str = " (#{extras.join(',')})"
      else
        extras_str = ""
      end
      components << "FINISHED#{extras_str}"
    end
  end
  components << "id: #{id}"
  components << "\"#{command}\""

  return "::Naf::HistoricalJob<#{components.join(', ')}>"
end

#verify_prerequisites(these_jobs) ⇒ Object

XXX This should go away (it was moved to ConstructionZone::Foreman)



291
292
293
294
295
296
297
298
299
# File 'app/models/naf/historical_job.rb', line 291

def verify_prerequisites(these_jobs)
  these_jobs.each do |this_job|
    if this_job.id == id
      raise JobPrerequisiteLoop.new(self)
    else
      verify_prerequisites(this_job.prerequisites)
    end
  end
end