Skip to content

Commit 8e9e9d5

Browse files
committed
WIP
1 parent 9999fe1 commit 8e9e9d5

File tree

9 files changed

+186
-7
lines changed

9 files changed

+186
-7
lines changed

async-container-demo/Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gem "async-container", path: "../"
6+
gem "debug"

async-container-demo/Gemfile.lock

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
PATH
2+
remote: ..
3+
specs:
4+
async-container (0.18.3)
5+
async (~> 2.10)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
async (2.21.1)
11+
console (~> 1.29)
12+
fiber-annotation
13+
io-event (~> 1.6, >= 1.6.5)
14+
console (1.29.0)
15+
fiber-annotation
16+
fiber-local (~> 1.1)
17+
json
18+
date (3.4.1)
19+
debug (1.9.2)
20+
irb (~> 1.10)
21+
reline (>= 0.3.8)
22+
fiber-annotation (0.2.0)
23+
fiber-local (1.1.0)
24+
fiber-storage
25+
fiber-storage (1.0.0)
26+
io-console (0.8.0)
27+
io-event (1.7.4)
28+
irb (1.14.1)
29+
rdoc (>= 4.0.0)
30+
reline (>= 0.4.2)
31+
json (2.9.0)
32+
psych (5.2.1)
33+
date
34+
stringio
35+
rdoc (6.8.1)
36+
psych (>= 4.0.0)
37+
reline (0.5.12)
38+
io-console (~> 0.5)
39+
stringio (3.1.2)
40+
41+
PLATFORMS
42+
arm64-darwin-22
43+
ruby
44+
45+
DEPENDENCIES
46+
async-container!
47+
debug
48+
49+
BUNDLED WITH
50+
2.5.22

async-container-demo/jobs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "console"
5+
6+
Console.logger.debug!
7+
8+
class Jobs
9+
LOG_FILE = File.join(Dir.pwd, "jobs.log")
10+
11+
def self.start = self.new.start
12+
13+
def start
14+
Console.debug(self, "Starting jobs...")
15+
16+
loop do
17+
Console.info(self, "Jobs running...")
18+
19+
sleep 1
20+
end
21+
rescue Interrupt
22+
Console.debug(self, "Exiting jobs...")
23+
exit
24+
end
25+
end
26+
27+
Jobs.start

async-container-demo/start

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "async/container"
5+
require "console"
6+
require "debug"
7+
8+
# container = Async::Container.new
9+
10+
# container.spawn(name: "Web") do |task|
11+
# task.exec("bundle", "exec", "web")
12+
# end
13+
14+
# container.spawn(name: "Jobs") do |task|
15+
# task.exec("bundle", "exec", "jobs")
16+
# end
17+
18+
Console.logger.debug!
19+
20+
class WebController < Async::Container::Controller
21+
def setup(container)
22+
container.spawn(name: "Web") do |instance|
23+
instance.exec("bundle", "exec", "web")
24+
end
25+
26+
# container.spawn(name: "Jobs") do |instance|
27+
# instance.exec("bundle", "exec", "jobs")
28+
# end
29+
end
30+
end
31+
32+
class App
33+
def self.start = self.new.start
34+
35+
def start
36+
Console.debug(self, "Starting container...")
37+
38+
web_controller = WebController.new
39+
web_controller.run
40+
end
41+
end
42+
43+
pid = Process.pid
44+
pgid = Process.getpgid(pid)
45+
46+
Thread.new do
47+
sleep 5
48+
Console.debug(self, "Sending HUP signal to restart container...")
49+
Process.kill("HUP", -pgid)
50+
51+
# sleep 5
52+
# Console.debug(self, "Sending INT signal to stop container...")
53+
# Process.kill("INT", Process.pid)
54+
end
55+
56+
App.start

async-container-demo/web

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "console"
5+
6+
Console.logger.debug!
7+
8+
class Web
9+
LOG_FILE = File.join(Dir.pwd, "web.log")
10+
11+
def self.start = self.new.start
12+
13+
def start
14+
Console.debug(self, "Starting web...")
15+
16+
loop do
17+
Console.info(self, "Web running...")
18+
19+
sleep 1
20+
end
21+
rescue Interrupt
22+
Console.debug(self, "Exiting web...")
23+
exit
24+
end
25+
end
26+
27+
Web.start

lib/async/container/controller.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,17 @@ def run
186186
# I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
187187
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
188188
interrupt_action = Signal.trap(:INT) do
189-
# $stderr.puts "Received INT signal, terminating...", caller
189+
$stderr.puts "Received INT signal, terminating...", caller
190190
::Thread.current.raise(Interrupt)
191191
end
192192

193193
terminate_action = Signal.trap(:TERM) do
194-
# $stderr.puts "Received TERM signal, terminating...", caller
194+
$stderr.puts "Received TERM signal, terminating...", caller
195195
::Thread.current.raise(Terminate)
196196
end
197197

198198
hangup_action = Signal.trap(:HUP) do
199-
# $stderr.puts "Received HUP signal, restarting...", caller
199+
$stderr.puts "Received HUP signal, restarting...", caller
200200
::Thread.current.raise(Hangup)
201201
end
202202

@@ -208,6 +208,7 @@ def run
208208
rescue SignalException => exception
209209
if handler = @signals[exception.signo]
210210
begin
211+
Console.debug(self, "Invoking signal handler for #{exception.signo}...", handler: handler)
211212
handler.call
212213
rescue SetupError => error
213214
Console.error(self) {error}

lib/async/container/generic.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,20 @@ def wait_until_ready
107107
Console.debug(self) do |buffer|
108108
buffer.puts "Waiting for ready:"
109109
@state.each do |child, state|
110-
buffer.puts "\t#{child.class}: #{state.inspect}"
110+
buffer.puts "\t#{child.inspect}: #{state}"
111111
end
112112
end
113113

114114
self.sleep
115115

116116
if self.status?(:ready)
117+
Console.logger.debug(self) do |buffer|
118+
buffer.puts "All ready:"
119+
@state.each do |child, state|
120+
buffer.puts "\t#{child.inspect}: #{state}"
121+
end
122+
end
123+
117124
return true
118125
end
119126
end

lib/async/container/group.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def wait_for(channel)
137137
protected
138138

139139
def wait_for_children(duration = nil)
140-
Console.debug(self, "Waiting for children...", duration: duration)
140+
Console.debug(self, "Waiting for children...", duration: duration, running: @running)
141+
141142
if !@running.empty?
142143
# Maybe consider using a proper event loop here:
143144
readable, _, _ = ::IO.select(@running.keys, nil, nil, duration)

lib/async/container/process.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ def name= value
122122

123123
# A human readable representation of the process.
124124
# @returns [String]
125-
def to_s
126-
"\#<#{self.class} #{@name}>"
125+
def inspect
126+
"\#<#{self.class} name=#{@name.inspect} status=#{@status.inspect} pid=#{@pid.inspect}>"
127127
end
128128

129+
alias to_s inspect
130+
129131
# Invoke {#terminate!} and then {#wait} for the child process to exit.
130132
def close
131133
self.terminate!
@@ -151,6 +153,8 @@ def terminate!
151153
# Wait for the child process to exit.
152154
# @returns [::Process::Status] The process exit status.
153155
def wait
156+
$stderr.puts "Waiting for #{@pid}...", caller
157+
154158
if @pid && @status.nil?
155159
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
156160

0 commit comments

Comments
 (0)