Skip to content

Commit 90eb1f6

Browse files
committed
Add Async::Container::Supervisor::Supervised environemnt for worker setup.
1 parent a1528a3 commit 90eb1f6

File tree

7 files changed

+100
-10
lines changed

7 files changed

+100
-10
lines changed

example/simple/simple.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def setup(container)
1212

1313
container.run(name: self.class.name, count: 4, restart: true, health_check_timeout: 2) do |instance|
1414
Async do
15-
Async::Container::Supervisor::Worker.new(instance, endpoint: @evaluator.supervisor_endpoint).run
15+
if @environment.implements?(Async::Container::Supervisor::Supervised)
16+
@evaluator.make_supervised_worker(instance).run
17+
end
1618

1719
start_time = Time.now
1820

@@ -37,7 +39,7 @@ def setup(container)
3739
service "sleep" do
3840
service_class SleepService
3941

40-
supervisor_endpoint {Async::Container::Supervisor.endpoint}
42+
include Async::Container::Supervisor::Supervised
4143
end
4244

4345
service "supervisor" do

lib/async/container/supervisor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
require_relative "supervisor/memory_monitor"
1313

1414
require_relative "supervisor/environment"
15-
require_relative "supervisor/service"
15+
require_relative "supervisor/supervised"

lib/async/container/supervisor/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run
5858
Console.error(self, "Connection failed:", exception: error)
5959
sleep(rand)
6060
ensure
61-
connection.close
61+
connection&.close
6262
end
6363
end
6464
end

lib/async/container/supervisor/environment.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
require "async/service/environment"
77

8+
require_relative "service"
9+
810
module Async
911
module Container
1012
module Supervisor
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require "async/service/environment"
7+
8+
module Async
9+
module Container
10+
module Supervisor
11+
module Supervised
12+
# The IPC path to use for communication with the supervisor.
13+
# @returns [String]
14+
def supervisor_ipc_path
15+
::File.expand_path("supervisor.ipc", root)
16+
end
17+
18+
# The endpoint the supervisor will bind to.
19+
# @returns [::IO::Endpoint::Generic]
20+
def supervisor_endpoint
21+
::IO::Endpoint.unix(supervisor_ipc_path)
22+
end
23+
24+
def make_supervised_worker(instance)
25+
Worker.new(instance, endpoint: supervisor_endpoint)
26+
end
27+
end
28+
end
29+
end
30+
end

test/async/container/supervised.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
require "async/container/supervisor/a_server"
7+
require "async/container/supervisor/supervised"
8+
9+
class SleepService < Async::Service::Generic
10+
def setup(container)
11+
super
12+
13+
container.spawn(name: self.class.name) do |instance|
14+
Async do
15+
if @environment.implements?(Async::Container::Supervisor::Supervised)
16+
@evaluator.make_supervised_worker(instance).run
17+
end
18+
19+
instance.ready!
20+
21+
sleep
22+
end
23+
end
24+
end
25+
end
26+
27+
describe Async::Container::Supervisor::Supervised do
28+
include Async::Container::Supervisor::AServer
29+
30+
let(:state) do
31+
{process_id: ::Process.pid}
32+
end
33+
34+
it "can define a supervised service" do
35+
environment = Async::Service::Environment.build(root: @root) do
36+
service_class {SimpleService}
37+
38+
include Async::Container::Supervisor::Supervised
39+
end
40+
41+
evaluator = environment.evaluator
42+
worker = evaluator.make_supervised_worker(state)
43+
worker_task = worker.run
44+
45+
sleep(0.001) until registration_monitor.registrations.any?
46+
47+
connection = registration_monitor.registrations.first
48+
expect(connection.state).to have_keys(
49+
process_id: be == ::Process.pid
50+
)
51+
ensure
52+
worker_task&.stop
53+
end
54+
end

test/async/container/supervisor.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
end
1414

1515
it "can connect to a server" do
16-
client = Async::Container::Supervisor::Worker.new(state, endpoint: endpoint)
17-
connection = client.connect
16+
worker = Async::Container::Supervisor::Worker.new(state, endpoint: endpoint)
17+
connection = worker.connect
1818

1919
# Wait for the client to connect to the server:
2020
sleep(0.001) until registration_monitor.registrations.any?
@@ -23,12 +23,14 @@
2323
expect(connection.state).to have_keys(
2424
process_id: be == ::Process.pid
2525
)
26+
ensure
27+
connection&.close
2628
end
2729

28-
with "do_memory_dump" do
30+
with "do: :memory_dump" do
2931
it "can dump memory" do
30-
client = Async::Container::Supervisor::Worker.new(state, endpoint: endpoint)
31-
client_task = client.run
32+
worker = Async::Container::Supervisor::Worker.new(state, endpoint: endpoint)
33+
worker_task = worker.run
3234

3335
sleep(0.001) until registration_monitor.registrations.any?
3436

@@ -38,7 +40,7 @@
3840

3941
expect(File.size(path)).to be > 0
4042
ensure
41-
client_task&.stop
43+
worker_task&.stop
4244
end
4345
end
4446
end

0 commit comments

Comments
 (0)