Class: Async::Container::Forked::Child

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

Overview

Represents a running child process from the point of view of the parent container.

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) ⇒ Child

Initialize the process.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/async/container/forked.rb', line 139

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

The name of the process.



181
182
183
# File 'lib/async/container/forked.rb', line 181

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



184
185
186
# File 'lib/async/container/forked.rb', line 184

def pid
  @pid
end

#The process identifier.(processidentifier.) ⇒ Object (readonly)



184
# File 'lib/async/container/forked.rb', line 184

attr :pid

Class Method Details

.fork(**options) ⇒ Object

Fork a child process appropriate for a container.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/async/container/forked.rb', line 99

def self.fork(**options)
	# $stderr.puts fork: caller
	self.new(**options) do |process|
		::Process.fork do
			# We use `Thread.current.raise(...)` so that exceptions are filtered through `Thread.handle_interrupt` correctly.
			Signal.trap(:INT) {::Thread.current.raise(Interrupt)}
			Signal.trap(:TERM) {::Thread.current.raise(Terminate)}
			Signal.trap(:HUP) {::Thread.current.raise(Restart)}
			
			# This could be a configuration option:
			::Thread.handle_interrupt(SignalException => :immediate) do
				yield Instance.for(process)
			rescue Interrupt
				# Graceful exit.
			rescue Exception => error
				Console.error(self, error)
				
				exit!(1)
			end
		end
	end
end

.spawn(*arguments, name: nil, **options) ⇒ Object

Spawn a child process using Process.spawn.

The child process will need to inform the parent process that it is ready using a notification protocol.



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

def self.spawn(*arguments, name: nil, **options)
	self.new(name: name) do |process|
		Notify::Pipe.new(process.out).before_spawn(arguments, options)
		
		::Process.spawn(*arguments, **options)
	end
end

Instance Method Details

#as_jsonObject

Convert the child process to a hash, suitable for serialization.



155
156
157
158
159
160
161
# File 'lib/async/container/forked.rb', line 155

def as_json(...)
	{
		name: @name,
		pid: @pid,
		status: @status&.to_i,
	}
end

#closeObject

Invoke #terminate! and then #wait for the child process to exit.



196
197
198
199
200
201
# File 'lib/async/container/forked.rb', line 196

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

#inspectObject Also known as: to_s

A human readable representation of the process.



188
189
190
# File 'lib/async/container/forked.rb', line 188

def inspect
	"\#<#{self.class} name=#{@name.inspect} status=#{@status.inspect} pid=#{@pid.inspect}>"
end

#interrupt!Object

Send ‘SIGINT` to the child process.



204
205
206
207
208
# File 'lib/async/container/forked.rb', line 204

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

#kill!Object

Send ‘SIGKILL` to the child process.



218
219
220
221
222
# File 'lib/async/container/forked.rb', line 218

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

#restart!Object

Send ‘SIGHUP` to the child process.



225
226
227
228
229
# File 'lib/async/container/forked.rb', line 225

def restart!
	unless @status
		::Process.kill(:HUP, @pid)
	end
end

#terminate!Object

Send ‘SIGTERM` to the child process.



211
212
213
214
215
# File 'lib/async/container/forked.rb', line 211

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

#to_jsonObject

Convert the request to JSON.



166
167
168
# File 'lib/async/container/forked.rb', line 166

def to_json(...)
	as_json.to_json(...)
end

#wait(timeout = 0.1) ⇒ Object

Wait for the child process to exit.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/async/container/forked.rb', line 236

def wait(timeout = 0.1)
	if @pid && @status.nil?
		Console.debug(self, "Waiting for process to exit...", pid: @pid)
		
		_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
		
		if @status.nil?
			sleep(timeout) if timeout
			
			_, @status = ::Process.wait2(@pid, ::Process::WNOHANG)
			
			if @status.nil?
				Console.warn(self) {"Process #{@pid} is blocking, sending kill signal..."}
				self.kill!
				
				# Wait for the process to exit:
				_, @status = ::Process.wait2(@pid)
			end
		end
	end
	
	Console.debug(self, "Process exited.", pid: @pid, status: @status)
	
	return @status
end