This repository has been archived by the owner on Dec 4, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rb
88 lines (64 loc) · 1.76 KB
/
build.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
82
83
84
85
86
87
88
class Build < ActiveRecord::Base
include Altria::Proprietary
serialize :properties, Hash
validates :job_id, presence: true
belongs_to :job, touch: true, counter_cache: true
scope :recent, -> { order("created_at DESC") }
scope :finished, -> { where("finished_at IS NOT NULL") }
scope :unfinished, -> { where("finished_at IS NULL") }
scope :started, -> { where("started_at IS NOT NULL") }
scope :running, -> { started.unfinished }
after_create { update_incremental_id }
validate :one_job_cannot_have_more_than_two_builds, on: :create
paginates_per 10
def self.latest
recent.first
end
# Pushes this build to worker's queue.
def enqueue
BuildWorker.perform_async(id)
end
# Runs this build, usually called from BuildWorker#perform.
def run
start
finish(job.run)
end
# Returns elapsed sec as a Float or nil.
def sec
finished_at - started_at if finished_at && started_at
end
def status_name
case status
when true
"success"
when false
"failure"
when nil
"unfinished"
end
end
private
def notify(type)
$redis.publish("build.#{type}", notified_data)
end
def notified_data
{ id: id, job_id: job.id, status: status_name, finished_at: finished_at }.to_json
end
def start
update_attributes!(started_at: Time.now)
notify(:started)
end
def finish(result)
reload.update_attributes!(finished_at: Time.now, output: result[:output], status: result[:status])
notify(:finished)
end
def update_incremental_id
update_attributes(incremental_id: job.builds_count + 1)
end
private
def one_job_cannot_have_more_than_two_builds
if job.builds.unfinished.any?
errors.add(:id, "One job cannot have more than two builds")
end
end
end