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



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

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



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

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

.canceledObject



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

def self.canceled
  where(request_to_terminate: true)
end

.connectionObject


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



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

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

.erroredObject



212
213
214
# File 'app/models/naf/historical_job.rb', line 212

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

.finishedObject



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

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

.full_table_name_prefixObject



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

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

.lock_for_job_queue(&block) ⇒ Object



216
217
218
# File 'app/models/naf/historical_job.rb', line 216

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

.partition_num_lead_buffersObject



168
169
170
# File 'app/models/naf/historical_job.rb', line 168

def self.partition_num_lead_buffers
  10
end

.partition_table_sizeObject



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

def self.partition_table_size
  100000
end

.queued_between(start_time, end_time) ⇒ Object



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

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

.queued_statusObject



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

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



208
209
210
# File 'app/models/naf/historical_job.rb', line 208

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

.running_statusObject



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

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

#affinity_idsObject



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

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

#historical_job_affinity_tabsObject



273
274
275
276
277
# File 'app/models/naf/historical_job.rb', line 273

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

#historical_job_prerequisitesObject



287
288
289
290
291
# File 'app/models/naf/historical_job.rb', line 287

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

#job_affinitiesObject



279
280
281
# File 'app/models/naf/historical_job.rb', line 279

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

#machine_started_on_server_addressObject



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

def machine_started_on_server_address
  started_on_machine.try(:server_address)
end

#machine_started_on_server_nameObject



265
266
267
# File 'app/models/naf/historical_job.rb', line 265

def machine_started_on_server_name
  started_on_machine.try(:server_name)
end

#prerequisitesObject



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

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

#spawnObject



312
313
314
# File 'app/models/naf/historical_job.rb', line 312

def spawn
  application_type.spawn(self)
end

#titleObject



261
262
263
# File 'app/models/naf/historical_job.rb', line 261

def title
  application.try(:title)
end

#to_sObject


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



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
249
250
251
252
253
254
255
256
257
258
259
# File 'app/models/naf/historical_job.rb', line 224

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)



302
303
304
305
306
307
308
309
310
# File 'app/models/naf/historical_job.rb', line 302

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