Class: Async::Container::Process

Inherits:
Channel
  • Object
show all
Defined in:
lib/async/container/process.rb

Defined Under Namespace

Classes: Instance

Instance Attribute Summary collapse

Attributes inherited from Channel

#in, #out

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Channel

#close_read, #close_write, #receive

Constructor Details

#initialize(name: nil) ⇒ Process

def self.spawn(*arguments, name: nil, **options) self.new(name: name) do |process| unless options.key?(:out) options[:out] = process.out end

    ::Process.spawn(*arguments, **options)
end

end



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/async/container/process.rb', line 97

def initialize(name: nil)
	super()
	
	@name = name
	@status = nil
	@pid = nil
	
	@pid = yield(self)
	
	# The parent process won't be writing to the channel:
	self.close_write
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



117
118
119
# File 'lib/async/container/process.rb', line 117

def name
  @name
end

Class Method Details

.fork(**options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/async/container/process.rb', line 68

def self.fork(**options)
	self.new(**options) do |process|
		::Process.fork do
			Signal.trap(:INT) {raise Interrupt}
			Signal.trap(:TERM) {raise Terminate}
			
			begin
				yield Instance.for(process)
			rescue Interrupt
				# Graceful exit.
			rescue Exception => error
				Async.logger.error(self) {error}
				
				exit!(1)
			end
		end
	end
end

Instance Method Details

#closeObject



129
130
131
132
133
134
# File 'lib/async/container/process.rb', line 129

def close
	self.terminate!
	self.wait
ensure
	super
end

#interrupt!Object



136
137
138
139
140
# File 'lib/async/container/process.rb', line 136

def interrupt!
	unless @status
		::Process.kill(:INT, @pid)
	end
end

#terminate!Object



142
143
144
145
146
# File 'lib/async/container/process.rb', line 142

def terminate!
	unless @status
		::Process.kill(:TERM, @pid)
	end
end

#to_sObject



119
120
121
122
123
124
125
126
127
# File 'lib/async/container/process.rb', line 119

def to_s
	if @status
		"\#<#{self.class} #{@name} -> #{@status}>"
	elsif @pid
		"\#<#{self.class} #{@name} -> #{@pid}>"
	else
		"\#<#{self.class} #{@name}>"
	end
end

#waitObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/async/container/process.rb', line 148

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
		
		if @status.nil?
			Async.logger.warn(self) {"Process #{@pid} is blocking, has it exited?"}
			_, @status = ::Process.wait2(@pid)
		end
	end
	
	return @status
end