Class: Servolux::Prefork::Worker

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(prefork) ⇒ Worker

Create a new worker that belongs to the prefork pool.

Parameters:

  • prefork (Prefork)

    The prefork pool that created this worker.


367
368
369
370
371
372
# File 'lib/servolux/prefork.rb', line 367

def initialize( prefork )
  @timeout = prefork.timeout
  @thread = nil
  @piper = nil
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error


361
362
363
# File 'lib/servolux/prefork.rb', line 361

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.

Returns:

  • (Boolean, nil)

436
437
438
439
# File 'lib/servolux/prefork.rb', line 436

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.

Parameters:

  • signal (String, Integer) (defaults to: 'TERM')

    The signal to send to the child process.

Returns:

  • (Integer, nil)

    The result of Process#kill or nil if called from the child process.


421
422
423
424
425
426
# File 'lib/servolux/prefork.rb', line 421

def signal( signal = 'TERM' )
  return if @piper.nil?
  @piper.signal signal
rescue Errno::ESRCH, Errno::ENOENT
  return nil
end

#startWorker

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.

Returns:


379
380
381
382
383
384
# File 'lib/servolux/prefork.rb', line 379

def start
  @error = nil
  @piper = ::Servolux::Piper.new('rw', :timeout => @timeout)
  @piper.parent? ? parent : child
  self
end

#stopWorker?

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.

Returns:


393
394
395
396
397
398
399
400
401
402
403
# File 'lib/servolux/prefork.rb', line 393

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)
  @thread = nil
  self
end

#timed_out?Boolean?

Returns true if communication with the child process timed out. Returns nil if the child process has not been started.

Always returns nil when called from the child process.

Returns:

  • (Boolean, nil)

448
449
450
451
# File 'lib/servolux/prefork.rb', line 448

def timed_out?
  return if @piper.nil? or @piper.child?
  CommunicationError === @error
end

#waitObject

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.


409
410
411
412
# File 'lib/servolux/prefork.rb', line 409

def wait
  return if @piper.nil? or @piper.child?
  @piper.wait(Process::WNOHANG|Process::WUNTRACED)
end