Class: Debci::Job

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Test::Expired

#expired?

Methods included from Test::Duration

#duration_human

Class Method Details

.amqp_channelObject



100
101
102
103
104
105
# File 'lib/debci/job.rb', line 100

def self.amqp_channel
  @conn ||= Bunny.new(Debci.config.amqp_server).tap do |conn|
    conn.start
  end
  @channel ||= @conn.create_channel
end

.get_queue(arch) ⇒ Object



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

def self.get_queue(arch)
  @queues ||= {}
  @queues[arch] ||=
    begin
      opts = {
        durable: true,
        arguments: {
          'x-max-priority': 10,
        }
      }
      q = ENV['debci_amqp_queue'] || "debci-tests-#{arch}-#{Debci.config.backend}"
      self.amqp_channel.queue(q, opts)
    end
end

.history(package, suite, arch) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/debci/job.rb', line 38

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/debci/job.rb', line 18

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)
  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



34
35
36
# File 'lib/debci/job.rb', line 34

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

Instance Method Details

#as_json(options = nil) ⇒ Object



63
64
65
# File 'lib/debci/job.rb', line 63

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

#enqueue(priority = 0) ⇒ Object



79
80
81
82
83
# File 'lib/debci/job.rb', line 79

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

#get_enqueue_parametersObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/debci/job.rb', line 67

def get_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

#prefixObject



46
47
48
49
50
# File 'lib/debci/job.rb', line 46

def prefix
  name = self.package
  name =~ /^((lib)?.)/
  $1
end

#timeObject

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



53
54
55
56
57
58
59
60
61
# File 'lib/debci/job.rb', line 53

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