Skip to content

Commit e2ce18a

Browse files
Honor configured graceful timeout in controller. (#65)
1 parent 518a754 commit e2ce18a

4 files changed

Lines changed: 46 additions & 15 deletions

File tree

lib/async/container/controller.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212

1313
module Async
1414
module Container
15+
# The default graceful stop policy for controllers.
16+
GRACEFUL_STOP = ENV.fetch("ASYNC_CONTAINER_GRACEFUL_STOP", "true").then do |value|
17+
case value
18+
when "true"
19+
true # Default timeout for graceful termination.
20+
when "false"
21+
false # Immediately kill the processes.
22+
else
23+
value.to_f
24+
end
25+
end
26+
1527
# Manages the life-cycle of one or more containers in order to support a persistent system.
1628
# e.g. a web server, job server or some other long running system.
1729
class Controller
@@ -23,7 +35,7 @@ class Controller
2335

2436
# Initialize the controller.
2537
# @parameter notify [Notify::Client] A client used for process readiness notifications.
26-
def initialize(notify: Notify.open!, container_class: Container, graceful_stop: true)
38+
def initialize(notify: Notify.open!, container_class: Container, graceful_stop: GRACEFUL_STOP)
2739
@notify = notify
2840
@container_class = container_class
2941
@graceful_stop = graceful_stop

lib/async/container/group.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,7 @@
1010

1111
module Async
1212
module Container
13-
# The default timeout for terminating processes, before escalating to killing.
14-
GRACEFUL_TIMEOUT = ENV.fetch("ASYNC_CONTAINER_GRACEFUL_TIMEOUT", "true").then do |value|
15-
case value
16-
when "true"
17-
true # Default timeout for graceful termination.
18-
when "false"
19-
false # Immediately kill the processes.
20-
else
21-
value.to_f
22-
end
23-
end
24-
25-
# The default timeout for graceful termination.
13+
# The default timeout for graceful termination, used when the `graceful` argument is true.
2614
DEFAULT_GRACEFUL_TIMEOUT = 10.0
2715

2816
# Manages a group of running processes.
@@ -163,7 +151,7 @@ def kill
163151
# If `graceful` is false, skip the SIGINT phase and go directly to SIGKILL.
164152
#
165153
# @parameter graceful [Boolean | Numeric] Whether to send SIGINT first or skip directly to SIGKILL.
166-
def stop(graceful = GRACEFUL_TIMEOUT)
154+
def stop(graceful = true)
167155
Console.debug(self, "Stopping all processes...", graceful: graceful)
168156

169157
# If a timeout is specified, interrupt the children first:

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Rename `ASYNC_CONTAINER_GRACEFUL_TIMEOUT` to `ASYNC_CONTAINER_GRACEFUL_STOP` and apply it at the controller level as `GRACEFUL_STOP`. `Group#stop` now only applies the shutdown policy it is given.
6+
37
## v0.36.0
48

59
- Forked containers now fork child processes from a short-lived thread, reducing inherited scheduler and parent stack state in children.

test/async/container/controller.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@
1717
end
1818
end
1919

20+
with "#graceful_stop" do
21+
def read_graceful_stop(value)
22+
command = [
23+
"bundle", "exec", "ruby", "-Ilib", "-e",
24+
"require 'async/container/controller'; puts Async::Container::Controller.new(notify: nil).graceful_stop.inspect"
25+
]
26+
27+
output = IO.popen({"ASYNC_CONTAINER_GRACEFUL_STOP" => value}, command, &:read)
28+
29+
expect($?.success?).to be == true
30+
31+
return output
32+
end
33+
34+
it "uses the configured graceful timeout by default" do
35+
output = read_graceful_stop("0.001")
36+
37+
expect(output).to be == "0.001\n"
38+
end
39+
40+
it "can disable graceful stop by default" do
41+
output = read_graceful_stop("false")
42+
43+
expect(output).to be == "false\n"
44+
end
45+
end
46+
2047
with "#reload" do
2148
it "can reuse keyed child" do
2249
input, output = IO.pipe

0 commit comments

Comments
 (0)