Class: Merb::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



6
7
8
# File 'lib/merb-core/dispatch/worker.rb', line 6

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

Instance Method Details

#process_queueObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/merb-core/dispatch/worker.rb', line 10

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