Skip to content

Commit 2c929d7

Browse files
committed
WIP.
1 parent 357b20a commit 2c929d7

File tree

5 files changed

+83
-21
lines changed

5 files changed

+83
-21
lines changed

examples/exec-child/jobs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ require "async/container/notify"
77
# Console.logger.debug!
88

99
class Jobs
10-
LOG_FILE = File.join(Dir.pwd, "jobs.log")
11-
1210
def self.start = self.new.start
1311

1412
def start
15-
Console.debug(self, "Starting jobs...")
13+
Console.info(self, "Starting jobs...")
1614

1715
if notify = Async::Container::Notify.open!
1816
Console.info(self, "Notifying container ready...")
@@ -22,11 +20,10 @@ class Jobs
2220
loop do
2321
Console.info(self, "Jobs running...")
2422

25-
sleep 1
23+
sleep 10
2624
end
27-
rescue Interrupt
28-
Console.debug(self, "Exiting jobs...")
29-
exit
25+
rescue Interrupt
26+
Console.info(self, "Exiting jobs...")
3027
end
3128
end
3229

examples/exec-child/readme.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Exec Child Example
2+
3+
This example demonstrates how to execute a child process using the `exec` function in a container.
4+
5+
## Usage
6+
7+
Start the main controller:
8+
9+
```
10+
> bundle exec ./start
11+
0.0s info: AppController [oid=0x938] [ec=0x94c] [pid=96758] [2024-12-12 14:33:45 +1300]
12+
| Controller starting...
13+
0.65s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96763] [2024-12-12 14:33:45 +1300]
14+
| Starting jobs...
15+
0.65s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96763] [2024-12-12 14:33:45 +1300]
16+
| Notifying container ready...
17+
0.65s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96763] [2024-12-12 14:33:45 +1300]
18+
| Jobs running...
19+
0.65s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96760] [2024-12-12 14:33:45 +1300]
20+
| Starting web...
21+
0.65s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96760] [2024-12-12 14:33:45 +1300]
22+
| Notifying container ready...
23+
0.65s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96760] [2024-12-12 14:33:45 +1300]
24+
| Web running...
25+
0.09s info: AppController [oid=0x938] [ec=0x94c] [pid=96758] [2024-12-12 14:33:45 +1300]
26+
| Controller started...
27+
```
28+
29+
In another terminal: `kill -HUP 96758` to cause a blue-green restart, which causes a new container to be started with new jobs and web processes:
30+
31+
```
32+
9.57s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96836] [2024-12-12 14:33:54 +1300]
33+
| Starting jobs...
34+
9.57s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96833] [2024-12-12 14:33:54 +1300]
35+
| Starting web...
36+
9.57s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96836] [2024-12-12 14:33:54 +1300]
37+
| Notifying container ready...
38+
9.57s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96833] [2024-12-12 14:33:54 +1300]
39+
| Notifying container ready...
40+
9.57s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96836] [2024-12-12 14:33:54 +1300]
41+
| Jobs running...
42+
9.57s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96833] [2024-12-12 14:33:54 +1300]
43+
| Web running...
44+
```
45+
46+
Once the new container is running and the child processes have notified they are ready, the controller will stop the old container:
47+
48+
```
49+
9.01s info: Async::Container::Group [oid=0xa00] [ec=0x94c] [pid=96758] [2024-12-12 14:33:54 +1300]
50+
| Stopping all processes...
51+
| {
52+
| "timeout": true
53+
| }
54+
9.01s info: Async::Container::Group [oid=0xa00] [ec=0x94c] [pid=96758] [2024-12-12 14:33:54 +1300]
55+
| Sending interrupt to 2 running processes...
56+
9.57s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96760] [2024-12-12 14:33:54 +1300]
57+
| Exiting web...
58+
9.57s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96763] [2024-12-12 14:33:54 +1300]
59+
| Exiting jobs...
60+
```
61+
62+
The new container continues to run as expected:
63+
64+
```
65+
19.57s info: Web [oid=0x8e8] [ec=0x8fc] [pid=96833] [2024-12-12 14:34:04 +1300]
66+
| Web running...
67+
19.57s info: Jobs [oid=0x8e8] [ec=0x8fc] [pid=96836] [2024-12-12 14:34:04 +1300]
68+
| Jobs running...
69+
```

examples/exec-child/web

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ require "async/container/notify"
77
# Console.logger.debug!
88

99
class Web
10-
LOG_FILE = File.join(Dir.pwd, "web.log")
11-
1210
def self.start = self.new.start
1311

1412
def start
15-
Console.debug(self, "Starting web...")
13+
Console.info(self, "Starting web...")
1614

1715
if notify = Async::Container::Notify.open!
1816
Console.info(self, "Notifying container ready...")
@@ -22,11 +20,10 @@ class Web
2220
loop do
2321
Console.info(self, "Web running...")
2422

25-
sleep 1
23+
sleep 10
2624
end
27-
rescue Interrupt
28-
Console.debug(self, "Exiting web...")
29-
exit
25+
rescue Interrupt
26+
Console.info(self, "Exiting web...")
3027
end
3128
end
3229

lib/async/container/controller.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ def initialize(notify: Notify.open!, container_class: Container, graceful_stop:
2626
@container = nil
2727
@container_class = container_class
2828

29-
if @notify = notify
30-
@notify.status!("Initializing...")
31-
end
32-
29+
@notify = notify
3330
@signals = {}
3431

35-
trap(SIGHUP) do
32+
self.trap(SIGHUP) do
3633
self.restart
3734
end
3835

@@ -188,6 +185,8 @@ def reload
188185

189186
# Enter the controller run loop, trapping `SIGINT` and `SIGTERM`.
190187
def run
188+
@notify&.status!("Initializing...")
189+
191190
# I thought this was the default... but it doesn't always raise an exception unless you do this explicitly.
192191
# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
193192
interrupt_action = Signal.trap(:INT) do

lib/async/container/notify/console.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def initialize(logger)
2424
end
2525

2626
# Send a message to the console.
27-
def send(level: :debug, **message)
28-
@logger.send(level, self) {message}
27+
def send(level: :info, **message)
28+
@logger.public_send(level, self) {message}
2929
end
3030

3131
# Send an error message to the console.

0 commit comments

Comments
 (0)