Skip to content

Commit a7b03e2

Browse files
Improve health check handling.
1 parent bbb0260 commit a7b03e2

4 files changed

Lines changed: 26 additions & 14 deletions

File tree

async-service.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
2727
spec.required_ruby_version = ">= 3.2"
2828

2929
spec.add_dependency "async"
30-
spec.add_dependency "async-container", "~> 0.28"
30+
spec.add_dependency "async-container", "~> 0.29"
3131
spec.add_dependency "string-format", "~> 0.2"
3232
end

lib/async/service/health_checker.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ def health_checker(instance, timeout = @evaluator.health_check_timeout, parent:
2828
# We deliberately create a fiber here, to confirm that fiber creation is working.
2929
# If something has gone wrong with fiber allocation, we will crash here, and that's okay.
3030
Fiber.new do
31-
instance.ready!
31+
instance.healthy!
3232
end.resume
3333

34-
sleep(timeout / 2)
34+
sleep(timeout / 2.0)
3535
end
3636
end
3737
else
3838
if block_given?
3939
yield(instance)
4040
end
4141

42-
instance.ready!
42+
instance.healthy!
4343
end
4444
end
4545
end

lib/async/service/managed_service.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,21 @@ def start
5959
# Override this method to emit metrics, logs, or perform other actions when the service preparation is complete.
6060
#
6161
# @parameter instance [Async::Container::Instance] The container instance.
62-
# @parameter start_time [Async::Clock] The monotonic start time from {Async::Clock.start}.
63-
def emit_prepared(instance, start_time)
62+
# @parameter clock [Async::Clock] The monotonic start time from {Async::Clock.start}.
63+
def emit_prepared(instance, clock)
6464
# Override in subclasses as needed.
65+
Console.info(self, "Prepared...", duration: clock.total)
6566
end
6667

6768
# Called after the service has started running.
6869
#
6970
# Override this method to emit metrics, logs, or perform other actions when the service begins running.
7071
#
7172
# @parameter instance [Async::Container::Instance] The container instance.
72-
# @parameter start_time [Async::Clock] The monotonic start time from {Async::Clock.start}.
73-
def emit_running(instance, start_time)
73+
# @parameter clock [Async::Clock] The monotonic start time from {Async::Clock.start}.
74+
def emit_running(instance, clock)
7475
# Override in subclasses as needed.
76+
Console.info(self, "Running...", duration: clock.total)
7577
end
7678

7779
# Set up the container with health checking and process title formatting.
@@ -83,22 +85,27 @@ def setup(container)
8385
health_check_timeout = container_options[:health_check_timeout]
8486

8587
container.run(**container_options) do |instance|
86-
start_time = Async::Clock.start
88+
clock = Async::Clock.start
8789

8890
Async do
8991
evaluator = self.environment.evaluator
92+
server = nil
93+
94+
health_checker(instance, health_check_timeout) do
95+
if server
96+
instance.name = format_title(evaluator, server)
97+
end
98+
end
9099

91100
instance.status!("Preparing...")
92101
evaluator.prepare!(instance)
93-
emit_prepared(instance, start_time)
102+
emit_prepared(instance, clock)
94103

95104
instance.status!("Running...")
96105
server = run(instance, evaluator)
97-
emit_running(instance, start_time)
106+
emit_running(instance, clock)
98107

99-
health_checker(instance) do
100-
instance.name = format_title(evaluator, server)
101-
end
108+
instance.ready!
102109
end
103110
end
104111
end

releases.md

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

3+
## Unreleased
4+
5+
- Start health checker earlier in the process. Use `#healthy!` message instead of `#ready!`.
6+
- Emit prepared and running log messages with durations (e.g. how long it took to transition to prepared and running states).
7+
38
## v0.17.0
49

510
- `ManagedService` now sends `status!` messages during startup to prevent premature health check timeouts for slow-starting services.

0 commit comments

Comments
 (0)