class EmWorkerPool::Worker

Attributes

args[R]
block[R]
pool[R]

Properties ===========================================================

Public Class Methods

new(pool, *args) click to toggle source

Creates a new worker attached to the provided pool. Optional arguments may be supplied, which are passed on to the blocks it processes.

# File lib/em_worker_pool/worker.rb, line 12
def initialize(pool, *args)
  @pool = pool
  @args = args

  @thread = Thread.new do
    Thread.abort_on_exception = true
    begin
      self.after_initialize

      while (block = @pool.block_pop)
        begin
          @block = block
          self.before_perform(block)
          perform(&block)
          self.after_perform(block)
          @block = nil

          unless (@pool.worker_needed?(self))
            @pool.worker_finished!(self)
            break
          end
        rescue => exception
          @pool.handle_exception(self, exception, block)
        end
      end
    rescue => exception
      @pool.handle_exception(self, exception, nil)
    end
  end
end

Public Instance Methods

after_initialize() click to toggle source

This method is called after the worker is initialized within the thread used by the worker. It can be customized in sub-classes as required.

# File lib/em_worker_pool/worker.rb, line 52
def after_initialize
end
after_perform(block) click to toggle source

This method is called just after the worker has finished executing the given block This should be customized in sub-classes to do any additional processing required.

# File lib/em_worker_pool/worker.rb, line 64
def after_perform(block)
end
before_perform(block) click to toggle source

This method is called just before the worker executes the given block. This should be customized in sub-classes to do any additional processing required.

# File lib/em_worker_pool/worker.rb, line 58
def before_perform(block)
end
join() click to toggle source

Called by the pool to reap this thread when it is finished.

# File lib/em_worker_pool/worker.rb, line 68
def join
  @thread.join
end
perform() { |*args| ... } click to toggle source

Calls the Proc pulled from the queue. Subclasses can implement their own method here which might pass in arguments to the block for contextual purposes.

# File lib/em_worker_pool/worker.rb, line 46
def perform
  yield(*@args)
end