forked from zendesk/samson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_execution.rb
176 lines (142 loc) · 4.13 KB
/
job_execution.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require 'thread_safe'
require 'shellwords'
class JobExecution
# Whether or not execution is enabled. This allows completely disabling job
# execution for testing purposes.
cattr_accessor(:enabled, instance_writer: false) do
Rails.application.config.samson.enable_job_execution
end
cattr_accessor(:lock_timeout, instance_writer: false) { 10.minutes }
cattr_reader(:registry, instance_accessor: false) { {} }
private_class_method :registry
attr_reader :output, :job, :viewers, :stage
def initialize(reference, job)
@output = OutputBuffer.new
@executor = TerminalExecutor.new(@output)
@viewers = JobViewers.new(@output)
@subscribers = []
@job, @reference = job, reference
@stage = @job.deploy.try(:stage)
@repository = @job.project.repository
end
def start!
ActiveRecord::Base.clear_active_connections!
@thread = Thread.new do
begin
run!
rescue => e
error!(e)
ensure
@output.close unless @output.closed?
ActiveRecord::Base.clear_active_connections!
JobExecution.finished_job(@job)
end
end
end
def wait!
@thread.try(:join)
end
def stop!
@executor.stop!
wait!
end
def subscribe(&block)
@subscribers << block
end
private
def error!(exception)
message = "JobExecution failed: #{exception.message}"
Airbrake.notify(exception,
error_message: message,
parameters: {
job_id: @job.id
}
)
@output.write(message + "\n")
@job.error! if @job.active?
end
def run!
@job.run!
output_aggregator = OutputAggregator.new(@output)
result = Dir.mktmpdir do |dir|
execute!(dir)
end
if result
@job.success!
else
@job.fail!
end
@output.close
@job.update_output!(output_aggregator.to_s)
@subscribers.each(&:call)
end
def execute!(dir)
unless setup!(dir)
@job.error!
return
end
FileUtils.mkdir_p(artifact_cache_dir)
@output.write("Executing deploy\n")
commands = [
"export DEPLOYER=#{@job.user.email.shellescape}",
"export DEPLOYER_EMAIL=#{@job.user.email.shellescape}",
"export DEPLOYER_NAME=#{@job.user.name.shellescape}",
"export REVISION=#{@reference.shellescape}",
"export TAG=#{(@job.tag || @job.commit).to_s.shellescape}",
"export CACHE_DIR=#{artifact_cache_dir}",
"cd #{dir}",
*@job.commands
]
payload = {
stage: (stage.try(:name) || "none"),
project: @job.project.name,
command: commands.join("\n")
}
ActiveRecord::Base.clear_active_connections!
ActiveSupport::Notifications.instrument("execute_shell.samson", payload) do
payload[:success] = @executor.execute!(*commands)
end
end
def setup!(dir)
locked = lock_project do
return false unless @repository.setup!(@executor, dir, @reference)
commit = @repository.commit_from_ref(@reference)
tag = @repository.tag_from_ref(@reference)
@job.update_git_references!(commit: commit, tag: tag)
end
if locked
true
else
@output.write("Could not get exclusive lock on repo. Maybe another stage is being deployed.\n")
false
end
end
def artifact_cache_dir
File.join(@repository.repo_cache_dir, "artifacts")
end
def lock_project(&block)
holder = (stage.try(:name) || @job.user.name)
callback = proc { |owner| output.write("Waiting for repository while setting it up for #{owner}\n") if Time.now.to_i % 10 == 0 }
@job.project.with_lock(output: @output, holder: holder, error_callback: callback, timeout: lock_timeout, &block)
end
class << self
def find_by_id(id)
registry[id.to_i]
end
def start_job(reference, job)
new(reference, job).tap do |job_execution|
if enabled
registry[job.id] = job_execution.tap(&:start!)
ActiveSupport::Notifications.instrument "job.threads", thread_count: registry.length
end
end
end
def all
registry.values
end
def finished_job(job)
registry.delete(job.id)
ActiveSupport::Notifications.instrument "job.threads", thread_count: registry.length
end
end
end