Class: Merb::Worker

Inherits:
Object show all
Defined in:
merb-core/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.



56
57
58
59
60
61
62
63
# File 'merb-core/lib/merb-core/dispatch/worker.rb', line 56

def initialize
  @thread = Thread.new do
    loop do
      process_queue
      break if Merb::Dispatcher.work_queue.empty? && Merb.exiting
    end
  end
end

Instance Attribute Details

#threadObject

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.



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

def thread
  @thread
end

Class Method Details

.alive?Boolean

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 Whether the Merb::Worker instance thread is alive

Returns:

  • (Boolean)

    Whether the Merb::Worker instance thread is alive



34
35
36
# File 'merb-core/lib/merb-core/dispatch/worker.rb', line 34

def alive?
  started? and @worker.thread.alive?
end

.restartMerb::Worker

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.

restarts the worker thread

Returns:



43
44
45
46
47
48
49
50
# File 'merb-core/lib/merb-core/dispatch/worker.rb', line 43

def restart
  # if we have a worker or thread, kill it.
  if started?
    @worker.thread.exit
    @worker = nil
  end
  start
end

.startMerb::Worker

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 instance of a worker.

Returns:



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

def start
  @worker ||= new
  Merb.at_exit do
    if Merb::Dispatcher.work_queue.empty?
      @worker.thread.abort_on_exception = false
      @worker.thread.raise
    else
      @worker.thread.join
    end
  end
  @worker
end

.started?Boolean

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 Whether the Merb::Worker instance is already started.

Returns:

  • (Boolean)

    Whether the Merb::Worker instance is already started.



27
28
29
# File 'merb-core/lib/merb-core/dispatch/worker.rb', line 27

def started?
  !@worker.nil?
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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'merb-core/lib/merb-core/dispatch/worker.rb', line 68

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
      break if Merb::Dispatcher.work_queue.empty? && Merb.exiting
    end
  rescue Exception => e
    Merb.logger.warn! %Q!Worker Thread Crashed with Exception:\n#{Merb.exception(e)}\nRestarting Worker Thread!
    retry
  end
end