Class: Debci::Job

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

Defined Under Namespace

Classes: InvalidStatusFile

Class Method Summary collapse

Instance Method Summary collapse

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



182
183
184
185
186
187
188
# File 'lib/debci/job.rb', line 182

def self.history(package, suite, arch)
  Debci::Job.finished.where(
    package: package,
    suite: suite,
    arch: arch
  ).order('date')
end

.import(status_file) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/debci/job.rb', line 90

def self.import(status_file)
  status = JSON.parse(File.read(status_file))
  run_id = status.delete('run_id').to_i
  package = status.delete('package')
  job = Debci::Job.find(run_id)
  if package != job.package.name
    raise InvalidStatusFile.new("Data in %{file} is for package %{pkg}, while database says that job %{id} is for package %{origpkg}" % {
      file: status_file,
      pkg: package,
      id: run_id,
      origpkg: job.package,
    })
  end
  status.each do |k, v|
    job.send("#{k}=", v)
  end

  job.save!
  job
end

.pendingObject



178
179
180
# File 'lib/debci/job.rb', line 178

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

.platform_specific_issuesObject



76
77
78
79
80
# File 'lib/debci/job.rb', line 76

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

.receive(directory) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/debci/job.rb', line 111

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

    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

    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.package.prefix / job.package.name / id
    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.save!

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

.status(exit_code) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/debci/job.rb', line 157

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

#always_failing?Boolean

Returns:

  • (Boolean)


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

def always_failing?
  last_pass_version.nil? || last_pass_version == 'n/a'
end

#as_json(options = nil) ⇒ Object



217
218
219
220
221
222
# File 'lib/debci/job.rb', line 217

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

#enqueue(priority = 0) ⇒ Object



236
237
238
239
240
# File 'lib/debci/job.rb', line 236

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

#enqueue_parametersObject



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/debci/job.rb', line 224

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

#had_success?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/debci/job.rb', line 258

def had_success?
  !always_failing?
end

#headlineObject



250
251
252
# File 'lib/debci/job.rb', line 250

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

#historyObject



190
191
192
# File 'lib/debci/job.rb', line 190

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

#last_passObject



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

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

#pinned?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/debci/job.rb', line 25

def pinned?
  !pin_packages.empty?
end

#previousObject



198
199
200
# File 'lib/debci/job.rb', line 198

def previous
  @previous ||= previous_unpinned_jobs.last
end

#previous_unpinned_jobsObject



194
195
196
# File 'lib/debci/job.rb', line 194

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

#timeObject

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



207
208
209
210
211
212
213
214
215
# File 'lib/debci/job.rb', line 207

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

#titleObject



246
247
248
# File 'lib/debci/job.rb', line 246

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

#to_sObject



242
243
244
# File 'lib/debci/job.rb', line 242

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