Class: Debci::Job

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

Defined Under Namespace

Classes: InvalidStatusFile

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Test::Prefix

#prefix

Methods included from Test::Paths

#autopkgtest_dir, #cleanup, #debci_log, #result_json, #root

Methods included from Test::Expired

#expired?

Methods included from Test::Duration

#duration_human

Class Method Details

.history(package, suite, arch) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/debci/job.rb', line 108

def self.history(package, suite, arch)
  Debci::Job.where(
    package: package,
    suite: suite,
    arch: arch
  ).where.not(status: nil).where(pin_packages: nil).order('date')
end

.import(status_file, suite, arch) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/debci/job.rb', line 24

def self.import(status_file, suite, arch)
  status = Debci::Status.from_file(status_file, suite, arch)
  status.run_id = status.run_id.to_i
  job = Debci::Job.find(status.run_id)
  if status.package != job.package
    raise InvalidStatusFile.new("Data in %{file} is for package %{pkg}, while database says that job %{id} is for package %{origpkg}" % {
      file: status_file,
      pkg: status.package,
      id: status.run_id,
      origpkg: job.package,
    })
  end
  job.duration_seconds = status.duration_seconds
  job.date = status.date
  job.last_pass_date = status.last_pass_date
  job.last_pass_version = status.last_pass_version
  job.message = status.message
  job.previous_status = status.previous_status
  job.version = status.version
  job.status = status.status
  job.save!
  job
end

.pendingObject



104
105
106
# File 'lib/debci/job.rb', line 104

def self.pending
  jobs = Debci::Job.where(status: nil).order(:created_at)
end

.receive(directory) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/debci/job.rb', line 48

def self.receive(directory)
  src = Pathname(directory)
  id = src.basename.to_s
  Debci::Job.find(id).tap do |job|
    job.status, job.message = status((src / 'exitcode').read.to_i)
    duration = (src / 'duration')
    job.duration_seconds = duration.read.to_i
    job.date = duration.stat.mtime
    job.version = (src / 'testpkg-version').read.split.last

    if job.previous
      job.previous_status = job.previous.status
    end
    if job.last_pass
      job.last_pass_date = job.last_pass.date
      job.last_pass_version = job.last_pass.version
    end

    base = Pathname(Debci.config.autopkgtest_basedir)
    dest = base / job.suite / job.arch / job.prefix / job.package / id
    dest.parent.mkpath
    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.save!

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

.status(exit_code) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/debci/job.rb', line 83

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



143
144
145
# File 'lib/debci/job.rb', line 143

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

#enqueue(priority = 0) ⇒ Object



159
160
161
162
163
# File 'lib/debci/job.rb', line 159

def enqueue(priority = 0)
  queue = Debci::AMQP.get_queue(arch)
  parameters = enqueue_parameters
  queue.publish("%s %s %s" % [package, suite, parameters.join(' ')], priority: priority)
end

#enqueue_parametersObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/debci/job.rb', line 147

def enqueue_parameters
  parameters = ['run-id:%s' % id]
  if self.trigger
    parameters << "trigger:#{CGI.escape(trigger)}"
  end
  Array(self.pin_packages).each do |pin|
    pkg, suite = pin
    parameters << "pin-packages:#{suite}=#{pkg}"
  end
  parameters
end

#historyObject



116
117
118
# File 'lib/debci/job.rb', line 116

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

#last_passObject



128
129
130
# File 'lib/debci/job.rb', line 128

def last_pass
  @last_pass ||= past.where(status: 'pass').last
end

#pastObject



120
121
122
# File 'lib/debci/job.rb', line 120

def past
  @past ||= history.where(["date < ?", date])
end

#previousObject



124
125
126
# File 'lib/debci/job.rb', line 124

def previous
  @previous ||= past.last
end

#timeObject

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



133
134
135
136
137
138
139
140
141
# File 'lib/debci/job.rb', line 133

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

#to_sObject



165
166
167
# File 'lib/debci/job.rb', line 165

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