Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/async/service/supervisor/supervised.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ def utilization_registry
end

# The supervised worker for the current process.
# @parameter state [Hash | Nil] Additional state to register with the supervisor.
# @returns [Worker] The worker client.
def supervisor_worker
def supervisor_worker(state: nil)
state = self.supervisor_worker_state.merge(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,
)
Expand All @@ -70,11 +73,12 @@ def supervisor_worker
# Create a supervised worker for the given instance.
#
# @parameter instance [Async::Container::Instance] The container instance.
# @parameter state [Hash | Nil] Additional state to register with the supervisor.
# @returns [Worker] The worker client.
def prepare!(instance)
def prepare!(instance, state: nil)
super(instance)

supervisor_worker.run
supervisor_worker(state: state).run
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Unreleased

- Allow supervised services to merge additional 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.
Expand Down
25 changes: 24 additions & 1 deletion test/async/service/supervised.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,28 @@ 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

additional_state = {
endpoint: {name: "http", addresses: [{address: "127.0.0.1", port: 9292}]},
}
worker = environment.evaluator.supervisor_worker(state: additional_state)
worker_task = worker.run

event = registration_monitor.pop
expect(event.supervisor_controller.state).to be == {
name: "simple-service",
endpoint: additional_state[:endpoint],
}
ensure
worker_task&.stop
end
end

Loading