Skip to content

Commit fae7eaa

Browse files
committed
Ensure process title is set immediately after run is invoked.
1 parent 9a9ad5f commit fae7eaa

3 files changed

Lines changed: 12 additions & 16 deletions

File tree

lib/async/service/managed/health_checker.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,28 @@ module Managed
1010
module HealthChecker
1111
# Start the health checker.
1212
#
13-
# If a timeout is specified, a transient child task will be scheduled, which will yield the instance if a block is given, then mark the instance as ready, and finally sleep for half the health check duration (so that we guarantee that the health check runs in time).
13+
# If a timeout is specified, a transient child task will be scheduled which will mark the instance as healthy, sleep for half the health check duration (so that we guarantee that the health check runs in time), and then yield the instance if a block is given. This repeats indefinitely.
1414
#
15-
# If a timeout is not specified, the health checker will yield the instance immediately and then mark the instance as ready.
15+
# If a timeout is not specified, the instance is marked as healthy immediately and no task is created. Any block given is ignored.
1616
#
1717
# @parameter instance [Object] The service instance to check.
1818
# @parameter timeout [Numeric] The timeout duration for the health check.
1919
# @parameter parent [Async::Task] The parent task to run the health checker in.
20-
# @yields {|instance| ...} If a block is given, it will be called with the service instance at least once.
20+
# @yields {|instance| ...} If a block is given and a timeout is specified, it will be called with the service instance after each sleep interval.
2121
def health_checker(instance, timeout = @evaluator.health_check_timeout, parent: Async::Task.current, &block)
2222
if timeout
2323
parent.async(transient: true) do
2424
while true
25-
if block_given?
26-
yield(instance)
27-
end
28-
2925
instance.healthy!
3026

3127
sleep(timeout / 2.0)
28+
29+
if block_given?
30+
yield(instance)
31+
end
3232
end
3333
end
3434
else
35-
if block_given?
36-
yield(instance)
37-
end
38-
3935
instance.healthy!
4036
end
4137
end

lib/async/service/managed/service.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def setup(container)
9393
evaluator = self.environment.evaluator
9494
server = nil
9595

96+
# If a health check timeout is configured, this block runs periodically to refresh the process title. Without a timeout there is no periodic timer, so the block is never called — the one-shot assignment below handles that case.
9697
health_checker(instance, health_check_timeout) do
9798
if server
9899
instance.name = format_title(evaluator, server)
@@ -105,6 +106,8 @@ def setup(container)
105106

106107
instance.status!("Running...")
107108
server = run(instance, evaluator)
109+
# Set the process title immediately once the server is available:
110+
instance.name = format_title(evaluator, server)
108111
emit_running(instance, clock)
109112

110113
instance.ready!

test/async/service/managed/health_checker.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,14 @@ def initialize(environment)
121121
expect(update[0]).to be == :healthy!
122122
end
123123

124-
it "yields the instance if a block is given" do
124+
it "does not yield the block" do
125125
block_called = false
126-
instance_passed = nil
127126

128127
service.health_checker(instance, nil) do |inst|
129128
block_called = true
130-
instance_passed = inst
131129
end
132130

133-
expect(block_called).to be == true
134-
expect(instance_passed).to be == instance
131+
expect(block_called).to be == false
135132
end
136133

137134
it "does not create async task when parent is not async" do

0 commit comments

Comments
 (0)