Skip to content

Commit

Permalink
Add exec child example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Dec 12, 2024
1 parent b7f0993 commit 357b20a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
33 changes: 33 additions & 0 deletions examples/exec-child/jobs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "console"
require "async/container/notify"

# Console.logger.debug!

class Jobs
LOG_FILE = File.join(Dir.pwd, "jobs.log")

def self.start = self.new.start

def start
Console.debug(self, "Starting jobs...")

if notify = Async::Container::Notify.open!
Console.info(self, "Notifying container ready...")
notify.ready!
end

loop do
Console.info(self, "Jobs running...")

sleep 1
end
rescue Interrupt
Console.debug(self, "Exiting jobs...")
exit
end
end

Jobs.start
24 changes: 24 additions & 0 deletions examples/exec-child/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "async/container"
require "console"

# Console.logger.debug!

class AppController < Async::Container::Controller
def setup(container)
container.spawn(name: "Web") do |instance|
# Specify ready: false here as the child process is expected to take care of the readiness notification:
instance.exec("bundle", "exec", "web", ready: false)
end

container.spawn(name: "Jobs") do |instance|
instance.exec("bundle", "exec", "jobs", ready: false)
end
end
end

controller = AppController.new

controller.run
33 changes: 33 additions & 0 deletions examples/exec-child/web
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "console"
require "async/container/notify"

# Console.logger.debug!

class Web
LOG_FILE = File.join(Dir.pwd, "web.log")

def self.start = self.new.start

def start
Console.debug(self, "Starting web...")

if notify = Async::Container::Notify.open!
Console.info(self, "Notifying container ready...")
notify.ready!
end

loop do
Console.info(self, "Web running...")

sleep 1
end
rescue Interrupt
Console.debug(self, "Exiting web...")
exit
end
end

Web.start
7 changes: 6 additions & 1 deletion lib/async/container/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ def setup(container)

# Start the container unless it's already running.
def start
self.restart unless @container
unless @container
Console.info(self) {"Controller starting..."}
self.restart
end

Console.info(self) {"Controller started..."}
end

# Stop the container if it's running.
Expand Down
7 changes: 4 additions & 3 deletions lib/async/container/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def wait
# Interrupt all running processes.
# This resumes the controlling fiber with an instance of {Interrupt}.
def interrupt
Console.debug(self, "Sending interrupt to #{@running.size} running processes...")
Console.info(self, "Sending interrupt to #{@running.size} running processes...")
@running.each_value do |fiber|
fiber.resume(Interrupt)
end
Expand All @@ -74,7 +74,7 @@ def interrupt
# Terminate all running processes.
# This resumes the controlling fiber with an instance of {Terminate}.
def terminate
Console.debug(self, "Sending terminate to #{@running.size} running processes...")
Console.info(self, "Sending terminate to #{@running.size} running processes...")
@running.each_value do |fiber|
fiber.resume(Terminate)
end
Expand All @@ -83,6 +83,7 @@ def terminate
# Stop all child processes using {#terminate}.
# @parameter timeout [Boolean | Numeric | Nil] If specified, invoke a graceful shutdown using {#interrupt} first.
def stop(timeout = 1)
Console.info(self, "Stopping all processes...", timeout: timeout)
# Use a default timeout if not specified:
timeout = 1 if timeout == true

Expand All @@ -105,7 +106,7 @@ def stop(timeout = 1)
end

# Terminate all children:
self.terminate
self.terminate if any?

# Wait for all children to exit:
self.wait
Expand Down
6 changes: 4 additions & 2 deletions lib/async/container/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ def wait
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)

while @status.nil?
Console.warn(self) {"Process #{@pid} is blocking, has it exited?"}

sleep(0.1)

_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)

if @status.nil?
Console.warn(self) {"Process #{@pid} is blocking, has it exited?"}
end
end
end

Expand Down

0 comments on commit 357b20a

Please sign in to comment.