Skip to content

Commit 357b20a

Browse files
committed
Add exec child example.
1 parent b7f0993 commit 357b20a

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed

examples/exec-child/jobs

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

examples/exec-child/start

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "async/container"
5+
require "console"
6+
7+
# Console.logger.debug!
8+
9+
class AppController < Async::Container::Controller
10+
def setup(container)
11+
container.spawn(name: "Web") do |instance|
12+
# Specify ready: false here as the child process is expected to take care of the readiness notification:
13+
instance.exec("bundle", "exec", "web", ready: false)
14+
end
15+
16+
container.spawn(name: "Jobs") do |instance|
17+
instance.exec("bundle", "exec", "jobs", ready: false)
18+
end
19+
end
20+
end
21+
22+
controller = AppController.new
23+
24+
controller.run

examples/exec-child/web

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

lib/async/container/controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ def setup(container)
9393

9494
# Start the container unless it's already running.
9595
def start
96-
self.restart unless @container
96+
unless @container
97+
Console.info(self) {"Controller starting..."}
98+
self.restart
99+
end
100+
101+
Console.info(self) {"Controller started..."}
97102
end
98103

99104
# Stop the container if it's running.

lib/async/container/group.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def wait
6565
# Interrupt all running processes.
6666
# This resumes the controlling fiber with an instance of {Interrupt}.
6767
def interrupt
68-
Console.debug(self, "Sending interrupt to #{@running.size} running processes...")
68+
Console.info(self, "Sending interrupt to #{@running.size} running processes...")
6969
@running.each_value do |fiber|
7070
fiber.resume(Interrupt)
7171
end
@@ -74,7 +74,7 @@ def interrupt
7474
# Terminate all running processes.
7575
# This resumes the controlling fiber with an instance of {Terminate}.
7676
def terminate
77-
Console.debug(self, "Sending terminate to #{@running.size} running processes...")
77+
Console.info(self, "Sending terminate to #{@running.size} running processes...")
7878
@running.each_value do |fiber|
7979
fiber.resume(Terminate)
8080
end
@@ -83,6 +83,7 @@ def terminate
8383
# Stop all child processes using {#terminate}.
8484
# @parameter timeout [Boolean | Numeric | Nil] If specified, invoke a graceful shutdown using {#interrupt} first.
8585
def stop(timeout = 1)
86+
Console.info(self, "Stopping all processes...", timeout: timeout)
8687
# Use a default timeout if not specified:
8788
timeout = 1 if timeout == true
8889

@@ -105,7 +106,7 @@ def stop(timeout = 1)
105106
end
106107

107108
# Terminate all children:
108-
self.terminate
109+
self.terminate if any?
109110

110111
# Wait for all children to exit:
111112
self.wait

lib/async/container/process.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ def wait
161161
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
162162

163163
while @status.nil?
164-
Console.warn(self) {"Process #{@pid} is blocking, has it exited?"}
165-
166164
sleep(0.1)
167165

168166
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
167+
168+
if @status.nil?
169+
Console.warn(self) {"Process #{@pid} is blocking, has it exited?"}
170+
end
169171
end
170172
end
171173

0 commit comments

Comments
 (0)