From a255ce65d4dbbec94560fea77002811fe27b5176 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 12 Dec 2024 13:53:39 +1300 Subject: [PATCH] WIP. --- async-container-demo/Gemfile.lock | 2 +- async-container-demo/start | 4 ++-- async-container-demo/web | 6 ++++++ lib/async/container/process.rb | 20 ++++++++++++-------- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/async-container-demo/Gemfile.lock b/async-container-demo/Gemfile.lock index f0f0f76..de496ef 100644 --- a/async-container-demo/Gemfile.lock +++ b/async-container-demo/Gemfile.lock @@ -11,7 +11,7 @@ GEM console (~> 1.29) fiber-annotation io-event (~> 1.6, >= 1.6.5) - console (1.29.0) + console (1.29.2) fiber-annotation fiber-local (~> 1.1) json diff --git a/async-container-demo/start b/async-container-demo/start index 6483221..5a15270 100755 --- a/async-container-demo/start +++ b/async-container-demo/start @@ -20,7 +20,7 @@ Console.logger.debug! class WebController < Async::Container::Controller def setup(container) container.spawn(name: "Web") do |instance| - instance.exec("bundle", "exec", "web") + instance.exec("bundle", "exec", "web", ready: false) end # container.spawn(name: "Jobs") do |instance| @@ -46,7 +46,7 @@ pgid = Process.getpgid(pid) Thread.new do sleep 5 Console.debug(self, "Sending HUP signal to restart container...") - Process.kill("HUP", -pgid) + Process.kill("HUP", pid) # sleep 5 # Console.debug(self, "Sending INT signal to stop container...") diff --git a/async-container-demo/web b/async-container-demo/web index a264c39..3c516c0 100755 --- a/async-container-demo/web +++ b/async-container-demo/web @@ -2,6 +2,7 @@ # frozen_string_literal: true require "console" +require "async/container/notify" Console.logger.debug! @@ -13,6 +14,11 @@ class Web 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...") diff --git a/lib/async/container/process.rb b/lib/async/container/process.rb index e61d442..bbaf09e 100644 --- a/lib/async/container/process.rb +++ b/lib/async/container/process.rb @@ -151,24 +151,28 @@ def terminate! end # Wait for the child process to exit. + # @asynchronous This method may block. + # # @returns [::Process::Status] The process exit status. def wait $stderr.puts "Waiting for #{@pid}...", caller if @pid && @status.nil? - _, @status = ::Process.wait2(@pid, ::Process::WNOHANG) + Console.debug(self, "Waiting for process to exit...", pid: @pid) - if @status.nil? - sleep(0.01) - _, @status = ::Process.wait2(@pid, ::Process::WNOHANG) - end - - if @status.nil? + _, @status = ::Process.wait2(@pid, ::Process::WNOHANG) + + while @status.nil? Console.warn(self) {"Process #{@pid} is blocking, has it exited?"} - _, @status = ::Process.wait2(@pid) + + sleep(0.1) + + _, @status = ::Process.wait2(@pid, ::Process::WNOHANG) end end + Console.debug(self, "Process exited.", pid: @pid, status: @status) + return @status end end