Class: Async::Container::Forked::Child
- 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
-
#name ⇒ Object
The name of the process.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
- #The process identifier.(processidentifier.) ⇒ Object readonly
Attributes inherited from Channel
Class Method Summary collapse
-
.fork(**options) ⇒ Object
Fork a child process appropriate for a container.
-
.spawn(*arguments, name: nil, **options) ⇒ Object
Spawn a child process using Process.spawn.
Instance Method Summary collapse
-
#as_json ⇒ Object
Convert the child process to a hash, suitable for serialization.
-
#close ⇒ Object
Invoke #terminate! and then #wait for the child process to exit.
-
#initialize(name: nil, **options) ⇒ Child
constructor
Initialize the process.
-
#inspect ⇒ Object
(also: #to_s)
A human readable representation of the process.
-
#interrupt! ⇒ Object
Send ‘SIGINT` to the child process.
-
#kill! ⇒ Object
Send ‘SIGKILL` to the child process.
-
#restart! ⇒ Object
Send ‘SIGHUP` to the child process.
-
#terminate! ⇒ Object
Send ‘SIGTERM` to the child process.
-
#to_json ⇒ Object
Convert the request to JSON.
-
#wait(timeout = 0.1) ⇒ Object
Wait for the child process to exit.
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, **) 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
#name ⇒ Object
The name of the process.
183 184 185 |
# File 'lib/async/container/forked.rb', line 183 def name @name end |
#pid ⇒ Object (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(**) # $stderr.puts fork: caller self.new(**) 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, **) self.new(name: name) do |process| Notify::Pipe.new(process.out).before_spawn(arguments, ) ::Process.spawn(*arguments, **) end end |
Instance Method Details
#as_json ⇒ Object
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 |
#close ⇒ Object
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 |
#inspect ⇒ Object 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_json ⇒ Object
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 |