Class: Merb::Worker

Inherits:
Object show all
Defined in:
lib/merb-core/dispatch/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new worker thread that loops over the work queue.



17
18
19
# File 'lib/merb-core/dispatch/worker.rb', line 17

def initialize
  @thread = Thread.new { loop { process_queue } }
end

Instance Attribute Details

#threadObject

Returns the value of attribute thread.



4
5
6
# File 'lib/merb-core/dispatch/worker.rb', line 4

def thread
  @thread
end

Class Method Details

.startObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns

Merb::Worker

instance of a worker.



10
11
12
# File 'lib/merb-core/dispatch/worker.rb', line 10

def self.start
  new
end

Instance Method Details

#process_queueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Processes tasks in the Merb::Dispatcher.work_queue.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/merb-core/dispatch/worker.rb', line 24

def process_queue
  begin
    while blk = Merb::Dispatcher.work_queue.pop
       # we've been blocking on the queue waiting for an item sleeping.
       # when someone pushes an item it wakes up this thread so we 
       # immediately pass execution to the scheduler so we don't 
       # accidentally run this block before the action finishes 
       # it's own processing
      Thread.pass
      blk.call
    end
  rescue Exception => e
    Merb.logger.warn! %Q!Worker Thread Crashed with Exception:\n#{Merb.exception(e)}\nRestarting Worker Thread!
    retry
  end
end