Skip to content

Commit f042735

Browse files
author
Jonathan Owens
committed
Wait longer for docker to kill containers during stop/restart
Passing identical timeouts to Docker and the Excon client resulted in Excon giving up too soon when Docker was trying to kill timed-out containers. For example, you may set a docker stop timeout of 30, expecting your service to respond adequately to a KILL after 30 seconds. However, your deploy would stop immediately after the 30 second timeout because Excon would give up right away. This adds 120 seconds to the given stop timeout, then passes that to the Excon client. This should give the deploy plenty of time for Docker to do its business even on sad servers. Followup to #171
1 parent ddd07d0 commit f042735

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

lib/centurion/docker_via_api.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def stop_container(container_id, timeout = 30)
5050
response = Excon.post(
5151
@base_uri + path,
5252
tls_excon_arguments.merge(
53-
read_timeout: timeout
53+
# Wait for both the docker stop timeout AND the kill AND
54+
# potentially a very slow HTTP server.
55+
read_timeout: timeout + 120
5456
)
5557
)
5658
raise response.inspect unless response.status == 204
@@ -94,7 +96,11 @@ def restart_container(container_id, timeout = 30)
9496
path = @docker_api_version + "/containers/#{container_id}/restart?t=#{timeout}"
9597
response = Excon.post(
9698
@base_uri + path,
97-
tls_excon_arguments
99+
tls_excon_arguments.merge(
100+
# Wait for both the docker stop timeout AND the kill AND
101+
# potentially a very slow HTTP server.
102+
read_timeout: timeout + 120
103+
)
98104
)
99105
case response.status
100106
when 204

lib/centurion/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Centurion
2-
VERSION = '1.8.7'
2+
VERSION = '1.8.9'
33
end

spec/docker_via_api_spec.rb

+11-8
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,29 @@
6363

6464
it 'stops a container' do
6565
expect(Excon).to receive(:post).
66-
with(excon_uri + "v1.12" + "/containers/12345/stop?t=300", {read_timeout: 300}).
66+
with(excon_uri + "v1.12" + "/containers/12345/stop?t=300", {read_timeout: 420}).
6767
and_return(double(status: 204))
6868
api.stop_container('12345', 300)
6969
end
7070

7171
it 'stops a container with a custom timeout' do
7272
expect(Excon).to receive(:post).
73-
with(excon_uri + "v1.12" + "/containers/12345/stop?t=30", {read_timeout: 30}).
73+
with(excon_uri + "v1.12" + "/containers/12345/stop?t=30", {read_timeout: 150}).
7474
and_return(double(status: 204))
7575
api.stop_container('12345')
7676
end
7777

7878
it 'restarts a container' do
7979
expect(Excon).to receive(:post).
80-
with(excon_uri + "v1.12" + "/containers/12345/restart?t=30", {}).
80+
with(excon_uri + "v1.12" + "/containers/12345/restart?t=30",
81+
{read_timeout: 150}).
8182
and_return(double(body: json_string, status: 204))
8283
api.restart_container('12345')
8384
end
8485

8586
it 'restarts a container with a custom timeout' do
8687
expect(Excon).to receive(:post).
87-
with(excon_uri + "v1.12" + "/containers/12345/restart?t=300", {}).
88+
with(excon_uri + "v1.12" + "/containers/12345/restart?t=300", {:read_timeout=>420}).
8889
and_return(double(body: json_string, status: 204))
8990
api.restart_container('12345', 300)
9091
end
@@ -179,7 +180,7 @@
179180
with(excon_uri + "v1.12" + "/containers/12345/stop?t=300",
180181
client_cert: '/certs/cert.pem',
181182
client_key: '/certs/key.pem',
182-
read_timeout: 300).
183+
read_timeout: 420).
183184
and_return(double(status: 204))
184185
api.stop_container('12345', 300)
185186
end
@@ -189,7 +190,7 @@
189190
with(excon_uri + "v1.12" + "/containers/12345/stop?t=30",
190191
client_cert: '/certs/cert.pem',
191192
client_key: '/certs/key.pem',
192-
read_timeout: 30).
193+
read_timeout: 150).
193194
and_return(double(status: 204))
194195
api.stop_container('12345')
195196
end
@@ -198,7 +199,8 @@
198199
expect(Excon).to receive(:post).
199200
with(excon_uri + "v1.12" + "/containers/12345/restart?t=30",
200201
client_cert: '/certs/cert.pem',
201-
client_key: '/certs/key.pem').
202+
client_key: '/certs/key.pem',
203+
read_timeout: 150).
202204
and_return(double(body: json_string, status: 204))
203205
api.restart_container('12345')
204206
end
@@ -207,7 +209,8 @@
207209
expect(Excon).to receive(:post).
208210
with(excon_uri + "v1.12" + "/containers/12345/restart?t=300",
209211
client_cert: '/certs/cert.pem',
210-
client_key: '/certs/key.pem').
212+
client_key: '/certs/key.pem',
213+
read_timeout: 420).
211214
and_return(double(body: json_string, status: 204))
212215
api.restart_container('12345', 300)
213216
end

0 commit comments

Comments
 (0)