Class: Async::WebDriver::Bridge::ProcessGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/async/webdriver/bridge/process_group.rb

Overview

A group of processes that are all killed when the group is closed.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ ProcessGroup

Create a new process group from an existing process id.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/async/webdriver/bridge/process_group.rb', line 22

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

Class Method Details

.spawn(*arguments) ⇒ Object

Spawn a new process group with a given command.



13
14
15
16
17
18
# File 'lib/async/webdriver/bridge/process_group.rb', line 13

def self.spawn(*arguments)
	# This might be problematic...
	self.new(
		::Process.spawn(*arguments, pgroup: true, out: File::NULL, err: File::NULL)
	)
end

Instance Method Details

#closeObject

Close the process group.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/webdriver/bridge/process_group.rb', line 37

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