class Servolux::Prefork::Worker

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.

Attributes

config[R]
error[R]

Public Class Methods

new( prefork, config ) click to toggle source

Create a new worker that belongs to the prefork pool.

@param [Prefork] prefork The prefork pool that created this worker. @param [Hash] config The worker configuration options.

# File lib/servolux/prefork.rb, line 372
def initialize( prefork, config )
  @timeout = prefork.timeout
  @config = config
  @thread = nil
  @piper = nil
  @error = nil
  @pid_list = []
end

Public Instance Methods

alive?() click to toggle source

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.

@return [Boolean, nil]

# File lib/servolux/prefork.rb, line 444
def alive?
  return if @piper.nil?
  @piper.alive?
end
kill( signal = 'TERM' ) click to toggle source
Alias for: signal
reap() click to toggle source

Internal: Attempt to reap any child processes spawned by this worker. If a child has exited, then we remove it from our PID list.

@return [Worker] this worker instance.

# File lib/servolux/prefork.rb, line 453
def reap
  @piper.alive? unless @piper.nil?
  @pid_list.dup.each do |pid|
    @pid_list.delete(pid) if reap?(pid)
  end
  self
end
reap?(pid) click to toggle source

Internal: Check the return status of the given child PID. This will reap the process from the kernel process table if the child has exited.

@return [Boolean] true if the PID has exited; false otherwise.

# File lib/servolux/prefork.rb, line 465
def reap?(pid)
  _, cstatus = Process.wait2(pid, Process::WNOHANG|Process::WUNTRACED)
  return true if cstatus
  Process.kill(0, pid)
  false
rescue Errno::ESRCH, Errno::ENOENT, Errno::ECHILD
  true
end
signal( signal = 'TERM' ) click to toggle source

Send this given signal to the child process. The default signal is ‘TERM’. This method will return immediately.

@param [String, Integer] signal The signal to send to the child process. @return [Integer, nil] The result of Process#kill or nil if called from

the child process.
# File lib/servolux/prefork.rb, line 429
def signal( signal = 'TERM' )
  return if @piper.nil?
  @piper.signal signal
rescue Errno::ESRCH, Errno::ENOENT
  return nil
end
Also aliased as: kill
start() click to toggle source

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.

@return [Worker] self

# File lib/servolux/prefork.rb, line 386
def start
  @pid_list << @piper.pid if @piper
  @error = nil
  @piper = ::Servolux::Piper.new('rw', :timeout => @timeout)
  @piper.parent? ? parent : child
  self
end
stop() click to toggle source

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.

@return [Worker, nil] self

# File lib/servolux/prefork.rb, line 401
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?() click to toggle source

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.

@return [Boolean, nil]

# File lib/servolux/prefork.rb, line 481
def timed_out?
  return if @piper.nil? or @piper.child?
  CommunicationError === @error
end
wait() click to toggle source

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.

# File lib/servolux/prefork.rb, line 417
def wait
  return if @piper.nil? or @piper.child?
  @piper.wait(Process::WUNTRACED)
end