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.



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

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

Returns:

  • (Boolean, nil)


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.

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.



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

#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:



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

#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:



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

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



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