Skip to content

Commit cd70709

Browse files
committed
Add some examples.
1 parent 0dd2b48 commit cd70709

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

examples/hang/service.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env async-service
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2025, by Samuel Williams.
6+
7+
require "async/container/supervisor"
8+
9+
class SleepService < Async::Service::Generic
10+
def setup(container)
11+
super
12+
13+
container.run(name: self.class.name, count: 1, restart: true, health_check_timeout: 2) do |instance|
14+
Async do
15+
if @environment.implements?(Async::Container::Supervisor::Supervised)
16+
@evaluator.make_supervised_worker(instance).run
17+
end
18+
19+
start_time = Time.now
20+
21+
instance.ready!
22+
23+
sleep # forever
24+
ensure
25+
Console.info(self, "Exiting...")
26+
end
27+
end
28+
end
29+
end
30+
31+
service "sleep" do
32+
service_class SleepService
33+
34+
include Async::Container::Supervisor::Supervised
35+
end
36+
37+
service "supervisor" do
38+
include Async::Container::Supervisor::Environment
39+
end

examples/memory-leak/service.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env async-service
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2025, by Samuel Williams.
6+
7+
require "async/container/supervisor"
8+
9+
class SleepService < Async::Service::Generic
10+
def setup(container)
11+
super
12+
13+
container.run(name: self.class.name, count: 4, restart: true, health_check_timeout: 2) do |instance|
14+
Async do
15+
if @environment.implements?(Async::Container::Supervisor::Supervised)
16+
@evaluator.make_supervised_worker(instance).run
17+
end
18+
19+
start_time = Time.now
20+
21+
instance.ready!
22+
23+
chunks = []
24+
while true
25+
Console.info(self, "Leaking memory...")
26+
chunks << " " * 1024 * 1024 * rand(10)
27+
sleep 1
28+
instance.ready!
29+
30+
uptime = Time.now - start_time
31+
instance.name = "Sleeping for #{uptime.to_i} seconds..."
32+
end
33+
ensure
34+
Console.info(self, "Exiting...")
35+
end
36+
end
37+
end
38+
end
39+
40+
service "sleep" do
41+
service_class SleepService
42+
43+
include Async::Container::Supervisor::Supervised
44+
end
45+
46+
service "supervisor" do
47+
include Async::Container::Supervisor::Environment
48+
49+
monitors do
50+
[Async::Container::Supervisor::MemoryMonitor.new(
51+
# The interval at which to check for memory leaks.
52+
interval: 1,
53+
# The total size limit of all processes:
54+
maximum_size_limit: 1024 * 1024 * 40, # 40 MB
55+
)]
56+
end
57+
end
File renamed without changes.

0 commit comments

Comments
 (0)