Skip to content

Commit 9f316b9

Browse files
committed
Fix Hybrid health check handling.
1 parent 2f9b706 commit 9f316b9

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

fixtures/async/container/a_container.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,39 @@ module Container
176176
expect(container).not.to be(:running?)
177177
end
178178
end
179+
180+
with "health_check_timeout:" do
181+
let(:container) {subject.new(health_check_interval: 0.01)}
182+
183+
it "should not terminate a child process if it updates its state within the specified time" do
184+
# We use #run here to hit the Hybrid container code path:
185+
container.run(count: 1, health_check_timeout: 0.02) do |instance|
186+
instance.ready!
187+
188+
10.times do
189+
instance.ready!
190+
sleep(0.01)
191+
end
192+
end
193+
194+
container.wait
195+
196+
expect(container.statistics).to have_attributes(failures: be == 0)
197+
end
198+
199+
it "can terminate a child process if it does not update its state within the specified time" do
200+
container.spawn(health_check_timeout: 0.01) do |instance|
201+
instance.ready!
202+
203+
# This should trigger the health check - since restart is false, the process will be terminated:
204+
sleep(1)
205+
end
206+
207+
container.wait
208+
209+
expect(container.statistics).to have_attributes(failures: be > 0)
210+
end
211+
end
179212
end
180213
end
181214
end

lib/async/container/hybrid.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Hybrid < Forked
1515
# @parameter count [Integer] The number of instances to start.
1616
# @parameter forks [Integer] The number of processes to fork.
1717
# @parameter threads [Integer] the number of threads to start.
18-
def run(count: nil, forks: nil, threads: nil, **options, &block)
18+
# @parameter health_check_timeout [Numeric] The timeout for health checks, in seconds. Passed into the child {Threaded} containers.
19+
def run(count: nil, forks: nil, threads: nil, health_check_timeout: nil, **options, &block)
1920
processor_count = Container.processor_count
2021
count ||= processor_count ** 2
2122
forks ||= [processor_count, count].min
@@ -25,7 +26,7 @@ def run(count: nil, forks: nil, threads: nil, **options, &block)
2526
self.spawn(**options) do |instance|
2627
container = Threaded.new
2728

28-
container.run(count: threads, **options, &block)
29+
container.run(count: threads, health_check_timeout: health_check_timeout, **options, &block)
2930

3031
container.wait_until_ready
3132
instance.ready!
@@ -34,6 +35,7 @@ def run(count: nil, forks: nil, threads: nil, **options, &block)
3435
rescue Async::Container::Terminate
3536
# Stop it immediately:
3637
container.stop(false)
38+
raise
3739
ensure
3840
# Stop it gracefully (also code path for Interrupt):
3941
container.stop

0 commit comments

Comments
 (0)