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.



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

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.



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

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

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



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

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
# File 'lib/async/container/forked.rb', line 101

def self.fork(**options)
	# $stderr.puts fork: caller
	self.new(**options) do |process|
		::Thread.new 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.value
	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.



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

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.



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

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

#closeObject

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



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

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

#inspectObject Also known as: to_s

A human readable representation of the process.



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

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

#interrupt!Object

Send ‘SIGINT` to the child process.



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

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

#kill!Object

Send ‘SIGKILL` to the child process.



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

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

#restart!Object

Send ‘SIGHUP` to the child process.



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

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

#terminate!Object

Send ‘SIGTERM` to the child process.



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

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

#to_jsonObject

Convert the request to JSON.



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

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

#wait(timeout = 0.1) ⇒ Object

Wait for the child process to exit.



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

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