Class: Servolux::Prefork::Worker
- Inherits:
-
Object
- Object
- Servolux::Prefork::Worker
- Defined in:
- lib/servolux/prefork.rb
Overview
The worker encapsulates the forking of the child process and communication between the parent and the child. Each worker instance is extended with the block or module supplied to the pre-forking pool that created the worker.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#alive? ⇒ Boolean?
Returns
true
if the child process is alive. -
#initialize(prefork) ⇒ Worker
constructor
Create a new worker that belongs to the prefork pool.
-
#signal(signal = 'TERM') ⇒ Integer?
(also: #kill)
Send this given signal to the child process.
-
#start ⇒ Worker
Start this worker.
-
#stop ⇒ Worker?
Stop this worker.
-
#wait ⇒ Object
Wait for the child process to exit.
Constructor Details
#initialize(prefork) ⇒ Worker
Create a new worker that belongs to the prefork pool.
275 276 277 278 279 280 281 |
# File 'lib/servolux/prefork.rb', line 275 def initialize( prefork ) @timeout = prefork.timeout @harvest = prefork.harvest @thread = nil @piper = nil @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
269 270 271 |
# File 'lib/servolux/prefork.rb', line 269 def error @error end |
Instance Method Details
#alive? ⇒ Boolean?
Returns true
if the child process is alive. Returns nil
if the child process has not been started.
Always returns nil
when called from the child process.
345 346 347 348 |
# File 'lib/servolux/prefork.rb', line 345 def alive? return if @piper.nil? @piper.alive? end |
#signal(signal = 'TERM') ⇒ Integer? Also known as: kill
Send this given signal to the child process. The default signal is ‘TERM’. This method will return immediately.
330 331 332 333 334 335 |
# File 'lib/servolux/prefork.rb', line 330 def signal( signal = 'TERM' ) return if @piper.nil? @piper.signal signal rescue Errno::ESRCH, Errno::ENOENT return nil end |
#start ⇒ Worker
Start this worker. A new process will be forked, and the code supplied by the user to the prefork pool will be executed in the child process.
288 289 290 291 292 293 |
# File 'lib/servolux/prefork.rb', line 288 def start @error = nil @piper = ::Servolux::Piper.new('rw', :timeout => @timeout) @piper.parent? ? parent : child self end |
#stop ⇒ Worker?
Stop this worker. The internal worker thread is stopped and a ‘HUP’ signal is sent to the child process. This method will return immediately without waiting for the child process to exit. Use the wait
method after calling stop
if your code needs to know when the child exits.
302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/servolux/prefork.rb', line 302 def stop return if @thread.nil? or @piper.nil? or @piper.child? @thread[:stop] = true @thread.wakeup if @thread.status close_parent signal 'TERM' @thread.join(0.5) rescue nil @thread = nil self end |
#wait ⇒ Object
Wait for the child process to exit. This method returns immediately when called from the child process or if the child process has not yet been forked.
318 319 320 321 |
# File 'lib/servolux/prefork.rb', line 318 def wait return if @piper.nil? or @piper.child? Process.wait(@piper.pid, Process::WNOHANG|Process::WUNTRACED) end |