Skip to content

Commit 1f362f0

Browse files
authored
Add draining for cc uploader (#4296)
1 parent 58ab34a commit 1f362f0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/cloud_controller/drain.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Drain
99
NGINX_FINAL_TIMEOUT = 10
1010
CCNG_FINAL_TIMEOUT = 20
1111
SLEEP_INTERVAL = 1
12+
CC_UPLOADER_FINAL_TIMEOUT_SECONDS = 900
1213

1314
def initialize(log_path)
1415
@log_path = log_path
@@ -57,6 +58,22 @@ def shutdown_delayed_worker(pid_path, timeout=15)
5758
send_signal('KILL', pid, process_name)
5859
end
5960

61+
def shutdown_cc_uploader(pid_path)
62+
pid = File.read(pid_path).to_i
63+
process_name = File.basename(pid_path, '.pid')
64+
# Initiate shutdown.
65+
send_signal('TERM', pid, process_name)
66+
67+
# Wait some additional time for cc_uploader to be terminated; otherwise write an error log message.
68+
log_shutdown_error(pid, process_name) unless wait_for_shutdown(pid, process_name, CC_UPLOADER_FINAL_TIMEOUT_SECONDS)
69+
70+
# force shutdown
71+
return if terminated?(pid, process_name)
72+
73+
log_info("Forcefully shutting down process '#{process_name}' with pid '#{pid}'")
74+
send_signal('KILL', pid, process_name)
75+
end
76+
6077
private
6178

6279
def send_signal(signal, pid, process_name)

spec/unit/lib/cloud_controller/drain_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,40 @@ def log_contents
159159
end
160160
end
161161
end
162+
163+
describe '#shutdown_cc_uploader' do
164+
it 'sends TERM to the cc_uploader process specified in the pid file' do
165+
expect(Process).to receive(:kill).with('TERM', pid)
166+
167+
drain.shutdown_cc_uploader(pid_path)
168+
169+
log_contents do |log|
170+
expect(log).to include("Sending signal 'TERM' to process '#{pid_name}' with pid '#{pid}'")
171+
end
172+
end
173+
174+
it 'waits 900s after sending TERM' do
175+
allow(Process).to receive(:getpgid).with(pid).and_return(1)
176+
177+
drain.shutdown_cc_uploader(pid_path)
178+
179+
expect(drain).to have_received(:sleep).exactly(900).times
180+
log_contents do |log|
181+
expect(log).to include("Process '#{pid_name}' with pid '#{pid}' is still running - this indicates an error in the shutdown procedure!")
182+
end
183+
end
184+
185+
it 'sends KILL to the cc_uploader process if it is still running after 900s' do
186+
allow(Process).to receive(:getpgid).with(pid).and_return(1)
187+
allow(Process).to receive(:kill).with('TERM', pid)
188+
expect(Process).to receive(:kill).with('KILL', pid)
189+
190+
drain.shutdown_cc_uploader(pid_path)
191+
192+
log_contents do |log|
193+
expect(log).to include("Forcefully shutting down process '#{pid_name}' with pid '#{pid}'")
194+
end
195+
end
196+
end
162197
end
163198
end

0 commit comments

Comments
 (0)