From ed1265760a341c9b21ace618d9032ac39cb3773a Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 23 Jul 2026 21:17:40 +1200 Subject: [PATCH 1/2] Allow explicit supervised worker state --- lib/async/service/supervisor/supervised.rb | 10 ++++++---- releases.md | 4 ++++ test/async/service/supervised.rb | 23 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/async/service/supervisor/supervised.rb b/lib/async/service/supervisor/supervised.rb index 3789760..30b9081 100644 --- a/lib/async/service/supervisor/supervised.rb +++ b/lib/async/service/supervisor/supervised.rb @@ -56,12 +56,13 @@ def utilization_registry end # The supervised worker for the current process. + # @parameter state [Hash] The state to register with the supervisor. # @returns [Worker] The worker client. - def supervisor_worker + def supervisor_worker(state: self.supervisor_worker_state) Worker.new( process_id: Process.pid, endpoint: supervisor_endpoint, - state: self.supervisor_worker_state, + state: state, utilization_schema: self.utilization_schema, utilization_registry: self.utilization_registry, ) @@ -70,11 +71,12 @@ def supervisor_worker # Create a supervised worker for the given instance. # # @parameter instance [Async::Container::Instance] The container instance. + # @parameter state [Hash] The state to register with the supervisor. # @returns [Worker] The worker client. - def prepare!(instance) + def prepare!(instance, state: self.supervisor_worker_state) super(instance) - supervisor_worker.run + supervisor_worker(state: state).run end end end diff --git a/releases.md b/releases.md index b37fe7e..085bdf8 100644 --- a/releases.md +++ b/releases.md @@ -1,5 +1,9 @@ # Releases +## Unreleased + + - Allow supervised services to supply explicit worker state during preparation and worker construction. + ## v0.17.0 - Add opt-in `metrics` and `traces` providers for supervisor process metrics, utilization metrics, and worker lifecycle tracing. diff --git a/test/async/service/supervised.rb b/test/async/service/supervised.rb index 502283c..10a0bae 100644 --- a/test/async/service/supervised.rb +++ b/test/async/service/supervised.rb @@ -56,5 +56,26 @@ def setup(container) ensure worker_task&.stop end + + it "can register explicit worker state" do + environment = Async::Service::Environment.build(root: @root) do + name "simple-service" + + service_class {SimpleService} + + include Async::Service::Supervisor::Supervised + end + + state = { + name: "simple-service", + endpoint: {name: "http", addresses: [{address: "127.0.0.1", port: 9292}]}, + } + worker = environment.evaluator.supervisor_worker(state: state) + worker_task = worker.run + + event = registration_monitor.pop + expect(event.supervisor_controller.state).to be == state + ensure + worker_task&.stop + end end - From c214c45cb75e7cefc5a785973e0f00a521069ec6 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 24 Jul 2026 11:20:07 +1200 Subject: [PATCH 2/2] Merge additional supervised worker state --- lib/async/service/supervisor/supervised.rb | 10 ++++++---- releases.md | 2 +- test/async/service/supervised.rb | 10 ++++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/async/service/supervisor/supervised.rb b/lib/async/service/supervisor/supervised.rb index 30b9081..62b2847 100644 --- a/lib/async/service/supervisor/supervised.rb +++ b/lib/async/service/supervisor/supervised.rb @@ -56,9 +56,11 @@ def utilization_registry end # The supervised worker for the current process. - # @parameter state [Hash] The state to register with the supervisor. + # @parameter state [Hash | Nil] Additional state to register with the supervisor. # @returns [Worker] The worker client. - def supervisor_worker(state: self.supervisor_worker_state) + def supervisor_worker(state: nil) + state = self.supervisor_worker_state.merge(state || {}) + Worker.new( process_id: Process.pid, endpoint: supervisor_endpoint, @@ -71,9 +73,9 @@ def supervisor_worker(state: self.supervisor_worker_state) # Create a supervised worker for the given instance. # # @parameter instance [Async::Container::Instance] The container instance. - # @parameter state [Hash] The state to register with the supervisor. + # @parameter state [Hash | Nil] Additional state to register with the supervisor. # @returns [Worker] The worker client. - def prepare!(instance, state: self.supervisor_worker_state) + def prepare!(instance, state: nil) super(instance) supervisor_worker(state: state).run diff --git a/releases.md b/releases.md index 085bdf8..7209132 100644 --- a/releases.md +++ b/releases.md @@ -2,7 +2,7 @@ ## Unreleased - - Allow supervised services to supply explicit worker state during preparation and worker construction. + - Allow supervised services to merge additional worker state during preparation and worker construction. ## v0.17.0 diff --git a/test/async/service/supervised.rb b/test/async/service/supervised.rb index 10a0bae..15636b6 100644 --- a/test/async/service/supervised.rb +++ b/test/async/service/supervised.rb @@ -66,15 +66,17 @@ def setup(container) include Async::Service::Supervisor::Supervised end - state = { - name: "simple-service", + additional_state = { endpoint: {name: "http", addresses: [{address: "127.0.0.1", port: 9292}]}, } - worker = environment.evaluator.supervisor_worker(state: state) + worker = environment.evaluator.supervisor_worker(state: additional_state) worker_task = worker.run event = registration_monitor.pop - expect(event.supervisor_controller.state).to be == state + expect(event.supervisor_controller.state).to be == { + name: "simple-service", + endpoint: additional_state[:endpoint], + } ensure worker_task&.stop end