Skip to content

Commit

Permalink
Improved logging when child process fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Dec 12, 2024
1 parent 9999fe1 commit b7f0993
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
9 changes: 8 additions & 1 deletion lib/async/container/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,20 @@ def wait_until_ready
Console.debug(self) do |buffer|
buffer.puts "Waiting for ready:"
@state.each do |child, state|
buffer.puts "\t#{child.class}: #{state.inspect}"
buffer.puts "\t#{child.inspect}: #{state}"
end
end

self.sleep

if self.status?(:ready)
Console.logger.debug(self) do |buffer|
buffer.puts "All ready:"
@state.each do |child, state|
buffer.puts "\t#{child.inspect}: #{state}"
end
end

return true
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/async/container/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def wait_for(channel)
protected

def wait_for_children(duration = nil)
Console.debug(self, "Waiting for children...", duration: duration)
Console.debug(self, "Waiting for children...", duration: duration, running: @running)

if !@running.empty?
# Maybe consider using a proper event loop here:
readable, _, _ = ::IO.select(@running.keys, nil, nil, duration)
Expand Down
26 changes: 16 additions & 10 deletions lib/async/container/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ def name= value

# A human readable representation of the process.
# @returns [String]
def to_s
"\#<#{self.class} #{@name}>"
def inspect
"\#<#{self.class} name=#{@name.inspect} status=#{@status.inspect} pid=#{@pid.inspect}>"
end

alias to_s inspect

# Invoke {#terminate!} and then {#wait} for the child process to exit.
def close
self.terminate!
Expand All @@ -149,22 +151,26 @@ def terminate!
end

# Wait for the child process to exit.
# @asynchronous This method may block.
#
# @returns [::Process::Status] The process exit status.
def wait
if @pid && @status.nil?
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)

if @status.nil?
sleep(0.01)
_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
end
Console.debug(self, "Waiting for process to exit...", pid: @pid)

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
Expand Down

0 comments on commit b7f0993

Please sign in to comment.