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.



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

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.



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

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

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



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

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

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(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
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.



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

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.



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

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

#closeObject

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



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

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

#inspectObject Also known as: to_s

A human readable representation of the process.



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

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

#interrupt!Object

Send ‘SIGINT` to the child process.



206
207
208
209
210
# File 'lib/async/container/forked.rb', line 206

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

#kill!Object

Send ‘SIGKILL` to the child process.



220
221
222
223
224
# File 'lib/async/container/forked.rb', line 220

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

#restart!Object

Send ‘SIGHUP` to the child process.



227
228
229
230
231
# File 'lib/async/container/forked.rb', line 227

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

#terminate!Object

Send ‘SIGTERM` to the child process.



213
214
215
216
217
# File 'lib/async/container/forked.rb', line 213

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

#to_jsonObject

Convert the request to JSON.



168
169
170
# File 'lib/async/container/forked.rb', line 168

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

#wait(timeout = 0.1) ⇒ Object

Wait for the child process to exit.



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

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