Class Merb::Worker
In: merb-core/lib/merb-core/dispatch/worker.rb
Parent: Object

Methods

new   process_queue   start  

Attributes

thread  [RW]  :api: private

Public Class methods

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

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/worker.rb, line 29
29:     def initialize
30:       @thread = Thread.new do
31:         loop do
32:           process_queue
33:           break if Merb::Dispatcher.work_queue.empty? && Merb.exiting
34:         end
35:       end
36:     end

Returns

Merb::Worker:instance of a worker.

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/worker.rb, line 12
12:       def start
13:         @worker ||= new
14:         Merb.at_exit do 
15:           if Merb::Dispatcher.work_queue.empty?
16:             @worker.thread.abort_on_exception = false
17:             @worker.thread.raise
18:           else
19:             @worker.thread.join
20:           end
21:         end
22:         @worker
23:       end

Public Instance methods

Processes tasks in the Merb::Dispatcher.work_queue.

:api: private

[Source]

    # File merb-core/lib/merb-core/dispatch/worker.rb, line 41
41:     def process_queue
42:       begin
43:         while blk = Merb::Dispatcher.work_queue.pop
44:            # we've been blocking on the queue waiting for an item sleeping.
45:            # when someone pushes an item it wakes up this thread so we 
46:            # immediately pass execution to the scheduler so we don't 
47:            # accidentally run this block before the action finishes 
48:            # it's own processing
49:           Thread.pass
50:           blk.call
51:           break if Merb::Dispatcher.work_queue.empty? && Merb.exiting
52:         end
53:       rescue Exception => e
54:         Merb.logger.warn! %Q!Worker Thread Crashed with Exception:\n#{Merb.exception(e)}\nRestarting Worker Thread!
55:         retry
56:       end
57:     end

[Validate]