Sha256: 2162f6efa4bb08aaefa61805cdb5a7a416f183b744c4d36b2940c418feb356f2

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

module Async
	module WebDriver
		module Bridge
			# A group of processes that are all killed when the group is closed.
			class ProcessGroup
				# Spawn a new process group with a given command.
				# @parameter arguments [Array] The command to execute.
				def self.spawn(*arguments)
					# This might be problematic...
					self.new(
						::Process.spawn(*arguments, pgroup: true, out: File::NULL, err: File::NULL)
					)
				end
				
				# Create a new process group from an existing process id.
				# @parameter pid [Integer] The process id.
				def initialize(pid)
					@pid = pid
					
					@status_task = Async(transient: true) do
						@status = ::Process.wait(@pid)
						
						unless @status.success?
							Console.error(self, "Process exited unexpectedly: #{@status}")
						end
					ensure
						self.close
					end
				end
				
				# Close the process group.
				def close
					if @status_task
						@status_task.stop
						@status_task = nil
					end
					
					if @pid
						::Process.kill("INT", -@pid)
						
						Async do |task|
							task.with_timeout(1) do
								::Process.wait(@pid)
							rescue Errno::ECHILD
								# Done.
							rescue Async::TimeoutError
								Console.info(self, "Killing pid #{@pid}...")
								::Process.kill("KILL", -@pid)
							end
						end.wait
						
						wait_all(-@pid)
						@pid = nil
					end
				end
				
				protected
				
				# Wait for all processes in the group to exit.
				def wait_all(pgid)
					while true
						pid, status = ::Process.wait2(pgid, ::Process::WNOHANG)
						
						break unless pid
					end
				rescue Errno::ECHILD
					# Done.
				end
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
async-webdriver-0.3.1 lib/async/webdriver/bridge/process_group.rb
async-webdriver-0.3.0 lib/async/webdriver/bridge/process_group.rb
async-webdriver-0.2.0 lib/async/webdriver/bridge/process_group.rb