Class: Debci::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Test::Duration, Test::Paths, Validators::APTSource
Defined in:
lib/debci/job.rb

Defined Under Namespace

Classes: InvalidStatusFile

Constant Summary collapse

PUBLIC_API =
{
  trigger: 'the same string that was provided in the test submission. (string)',
  package: 'name of tested package (string)',
  arch: 'architecture where the test ran (string)',
  suite: 'suite where the test ran (string)',
  version: 'version of the package that was tested (string)',
  status: '"pass", "fail", or "tmpfail" (string), or *null* if the test didn\'t finish yet.',
  run_id: 'an id for the test run, generated by debci (integer)',
  is_private: 'bool value to specify whether test is private or not',
  extra_apt_sources: 'an array specifying extra apt sources added to `/etc/apt/sources.list.d` for the test.',
  updated_at: 'timestamp of the last update to the job (string)',
  date: 'timestamp of when the test run finished (string)',
  duration_seconds: 'duration of the test run, in seconds (integer)',
}.freeze
DEFAULT_PRIORITY =
5

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Test::Paths

#autopkgtest_dir, #debci_log, #purge_files, #result_json, #root

Methods included from Test::Duration

#duration_human

Methods included from Validators::APTSource

#invalid_extra_apt_sources

Class Method Details

.cancel ⇒ Object



338
339
340
341
342
343
344
# File 'lib/debci/job.rb', line 338

def self.cancel
  update_all(
    date: Time.now,
    status: 'tmpfail',
    files_purged: true,
  )
end

.history(package, suite, arch) ⇒ Object



259
260
261
262
263
264
265
# File 'lib/debci/job.rb', line 259

def self.history(package, suite, arch)
  Debci::Job.includes(:requestor).not_private.where(
    package: package,
    suite: suite,
    arch: arch
  )
end

.pending ⇒ Object



255
256
257
# File 'lib/debci/job.rb', line 255

def self.pending
  Debci::Job.includes(:requestor).not_private.where(status: nil).order(:created_at)
end

.platform_specific_issues ⇒ Object



124
125
126
127
128
129
130
# File 'lib/debci/job.rb', line 124

def self.platform_specific_issues
  all_status.includes(:package).where(
    packages: { removed: false }
  ).group_by(&:package).select do |_, statuses|
    statuses.map(&:status).uniq.size > 1
  end
end

.receive(directory) ⇒ Object



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
# File 'lib/debci/job.rb', line 140

def self.receive(directory)
  src = Pathname(directory)
  id = src.basename.to_s
  begin
    job = Debci::Job.find(id)
  rescue ActiveRecord::RecordNotFound
    nil
  else
    exitcode = src / 'exitcode'
    unless exitcode.exist?
      job.status = "tmpfail"
      job.message = "Invalid test results received"
      return job
    end
    job.status, job.message = status(exitcode.read.to_i)
    duration = (src / 'duration')
    if duration.exist?
      job.duration_seconds = duration.read.to_i
      job.date = duration.stat.mtime
    else
      job.duration_seconds = 0
      job.date = Time.now
    end

    worker_file = (src / 'worker')
    if worker_file.exist?
      job.worker = Debci::Worker.find_or_create_by!(name: worker_file.read.strip)
    end

    testpkg_version = src / 'testpkg-version'
    if testpkg_version.exist?
      job.version = testpkg_version.read.split.last if testpkg_version
    else
      job.version = 'n/a'
    end

    dest = job.autopkgtest_data_dir
    dest.parent.mkpath

    # remove destination directory if it exists; this can happen is a
    # previous receiving was interrupted (e.g. if the daemon is restarte)
    dest.rmtree if dest.exist?

    FileUtils.cp_r src, dest
    Dir.chdir dest do
      artifacts = Dir['*'] - ['log.gz']
      cmd = ['tar', '-caf', 'artifacts.tar.gz', '--remove-files', '--', *artifacts]
      system(*cmd) || raise('Command failed: %<cmd>s' % { cmd: cmd.join(' ') })
    end
    job.calculate_file_sizes!

    job.received_at = Time.now

    job.save!

    # only remove original directory after everything went well
    src.rmtree
    job
  end
end

.status(exit_code) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/debci/job.rb', line 234

def self.status(exit_code)
  case exit_code
  when 0
    ['pass', 'All tests passed']
  when 2
    ['pass', 'Tests passed, but at least one test skipped']
  when 4
    ['fail', 'Tests failed']
  when 6
    ['fail', 'Tests failed, and at least one test skipped']
  when 12, 14
    ['fail', 'Erroneous package']
  when 8
    ['neutral', 'No tests in this package or all skipped']
  when 16
    ['tmpfail', 'Could not run tests due to a temporary testbed failure']
  else
    ['tmpfail', "Unexpected autopkgtest exit code #{exit_code}"]
  end
end

Instance Method Details

#as_json(options = nil) ⇒ Object



282
283
284
285
286
287
# File 'lib/debci/job.rb', line 282

def as_json(options = nil)
  super.update(
    "duration_human" => self.duration_human,
    "package" => package.name,
  )
end

#autopkgtest_data_dir ⇒ Object



205
206
207
# File 'lib/debci/job.rb', line 205

def autopkgtest_data_dir
  @autopkgtest_data_dir ||= Pathname(Debci.config.autopkgtest_basedir) / autopkgtest_data_path
end

#autopkgtest_data_path ⇒ Object



201
202
203
# File 'lib/debci/job.rb', line 201

def autopkgtest_data_path
  @autopkgtest_data_path ||= File.join(suite, arch, package.prefix, package.name, id.to_s)
end

#backend ⇒ Object



301
302
303
# File 'lib/debci/job.rb', line 301

def backend
  selected_backend || requested_backend || Debci::Backend.default
end

#calculate_file_sizes! ⇒ Object



223
224
225
226
227
228
# File 'lib/debci/job.rb', line 223

def calculate_file_sizes!
  dir = self.autopkgtest_data_dir
  log = (dir / 'log.gz')
  self.log_size = log.size if log.exist?
  self.artifacts_size = (dir / 'artifacts.tar.gz').size
end

#cleanup(reason: "no reason given") ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/debci/job.rb', line 211

def cleanup(reason: "no reason given")
  self.purge_files
  self.update_columns(files_purged: true)
  Debci.log(
    'Cleaned up files for job %<job_id>s (%<job>s): %<reason>s' % {
      job_id: self.run_id,
      job: self,
      reason: reason,
    }
  )
end

#disk_usage ⇒ Object



230
231
232
# File 'lib/debci/job.rb', line 230

def disk_usage
  (self.log_size || 0) + (self.artifacts_size || 0)
end

#enqueue(priority = nil) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/debci/job.rb', line 291

def enqueue(priority = nil)
  runner = Debci::Runner.get(self)

  self.selected_backend = runner.select_backend
  self.priority = priority || self.priority || DEFAULT_PRIORITY
  save!

  runner.submit
end

#headline ⇒ Object



334
335
336
# File 'lib/debci/job.rb', line 334

def headline
  "#{package.name} #{version} #{status.upcase} on #{suite}/#{arch}"
end

#history ⇒ Object



267
268
269
# File 'lib/debci/job.rb', line 267

def history
  @history ||= self.class.history(package, suite, arch).order('date')
end

#pinned? ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/debci/job.rb', line 67

def pinned?
  !pin_packages.empty?
end

#preferred_backend ⇒ Object



305
306
307
# File 'lib/debci/job.rb', line 305

def preferred_backend
  requested_backend || package&.backend
end

#public_api_attributes ⇒ Object



33
34
35
36
37
38
39
# File 'lib/debci/job.rb', line 33

def public_api_attributes
  data = PUBLIC_API.to_h do |k, _|
    [k, self[k]]
  end
  data[:package] = package.name
  data
end

#retry ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/debci/job.rb', line 309

def retry
  new_job = Debci::Job.create!(
    package: self.package,
    suite: self.suite,
    arch: self.arch,
    autopkgtest: autopkgtest,
    requested_backend: requested_backend,
    requestor: self.requestor,
    trigger: self.trigger,
    pin_packages: self.pin_packages,
    is_private: self.is_private,
    extra_apt_sources: self.extra_apt_sources
  )
  new_job.enqueue
  new_job
end

#time ⇒ Object

Returns the amount of time since the date for this status object



272
273
274
275
276
277
278
279
280
# File 'lib/debci/job.rb', line 272

def time
  days = (Time.now - self.created_at)/86400

  if days >= 1 || days <= -1
    "#{days.floor} day(s) ago"
  else
    "#{Time.at(Time.now - self.created_at).gmtime.strftime('%H')} hour(s) ago"
  end
end

#title ⇒ Object



330
331
332
# File 'lib/debci/job.rb', line 330

def title
  '%s %s' % [version, status]
end

#to_s ⇒ Object



326
327
328
# File 'lib/debci/job.rb', line 326

def to_s
  "%s %s/%s (%s)" % [package.name, suite, arch, status || 'pending']
end

#update_package_status ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/debci/job.rb', line 103

def update_package_status
  return if is_private
  return unless status
  return unless date
  return if pinned?

  transaction do
    next if history.where(['date > ?', date]).exists?

    status = Debci::PackageStatus.find_or_initialize_by(
      package: self.package,
      suite: self.suite,
      arch: self.arch,
    )
    status.job = self
    status.save!
  end

  nil
end