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, **options) ⇒ Child

Initialize the process.



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/async/container/forked.rb', line 144

def initialize(name: nil, **options)
	super(**options)
	
	@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.



186
187
188
# File 'lib/async/container/forked.rb', line 186

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

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



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

attr :pid

Class Method Details

.fork(**options) ⇒ Object

Fork a child process appropriate for a container.



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

def self.fork(**options)
	# $stderr.puts fork: caller
	self.new(**options) do |process|
		# CRuby makes the currently executing fiber the root fiber in the child process and clears its resuming fiber, so the caller's stack can be collected without forking from a separate native thread:
		::Fiber.new(blocking: true) do
			::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(Interrupt)}  # Same as SIGINT.
				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.resume
	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.



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

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.



160
161
162
163
164
165
166
# File 'lib/async/container/forked.rb', line 160

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

#closeObject

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



201
202
203
204
205
206
# File 'lib/async/container/forked.rb', line 201

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

#inspectObject Also known as: to_s

A human readable representation of the process.



193
194
195
# File 'lib/async/container/forked.rb', line 193

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

#interrupt!Object

Send SIGINT to the child process.



209
210
211
212
213
# File 'lib/async/container/forked.rb', line 209

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

#kill!Object

Send SIGKILL to the child process.



223
224
225
226
227
# File 'lib/async/container/forked.rb', line 223

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

#restart!Object

Send SIGHUP to the child process.



230
231
232
233
234
# File 'lib/async/container/forked.rb', line 230

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

#terminate!Object

Send SIGTERM to the child process.



216
217
218
219
220
# File 'lib/async/container/forked.rb', line 216

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

#to_jsonObject

Convert the request to JSON.



171
172
173
# File 'lib/async/container/forked.rb', line 171

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

#wait(timeout = 0.1) ⇒ Object

Wait for the child process to exit.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/async/container/forked.rb', line 241

def wait(timeout = 0.1)
	if @pid && @status.nil?
		Console.debug(self, "Waiting for process to exit...", child: {process_id: @pid}, timeout: timeout)
		
		_, @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 is blocking, sending kill signal...", child: {process_id: @pid}, timeout: timeout)
				self.kill!
				
				# Wait for the process to exit:
				_, @status = ::Process.wait2(@pid)
			end
		end
	end
	
	Console.debug(self, "Process exited.", child: {process_id: @pid, status: @status})
	
	return @status
end