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



184
185
186
187
188
# File 'app/models/naf/historical_job.rb', line 184

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



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

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



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

def self.canceled
  where(request_to_terminate: true)
end

.connectionObject


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



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

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

.erroredObject



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

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

.finishedObject



190
191
192
# File 'app/models/naf/historical_job.rb', line 190

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

.full_table_name_prefixObject



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

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

.lock_for_job_queue(&block) ⇒ Object



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

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

.partition_num_lead_buffersObject



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

def self.partition_num_lead_buffers
  10
end

.partition_table_sizeObject



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

def self.partition_table_size
  100000
end

.queued_between(start_time, end_time) ⇒ Object



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

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

.queued_statusObject



194
195
196
197
198
# File 'app/models/naf/historical_job.rb', line 194

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



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

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

.running_statusObject



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

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



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

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

#historical_job_affinity_tabsObject



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

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

#historical_job_prerequisitesObject



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

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

#job_affinitiesObject



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

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

#machine_started_on_server_addressObject



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

def machine_started_on_server_address
  started_on_machine.try(:server_address)
end

#machine_started_on_server_nameObject



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

def machine_started_on_server_name
  started_on_machine.try(:server_name)
end

#prerequisitesObject



290
291
292
293
294
295
# File 'app/models/naf/historical_job.rb', line 290

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



309
310
311
# File 'app/models/naf/historical_job.rb', line 309

def spawn
  application_type.spawn(self)
end

#titleObject



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

def title
  application.try(:title)
end

#to_sObject


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



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

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)



299
300
301
302
303
304
305
306
307
# File 'app/models/naf/historical_job.rb', line 299

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