Class: Merb::Worker
Instance Attribute Summary collapse
-
#thread ⇒ Object
:api: private.
Class Method Summary collapse
-
.alive? ⇒ Boolean
Returns Whether the Merb::Worker instance thread is alive.
-
.restart ⇒ Object
restarts the worker thread.
-
.start ⇒ Object
Returns Merb::Worker:: instance of a worker.
-
.started? ⇒ Boolean
Returns Whether the Merb::Worker instance is already started.
Instance Method Summary collapse
-
#initialize ⇒ Worker
constructor
Creates a new worker thread that loops over the work queue.
-
#process_queue ⇒ Object
Processes tasks in the Merb::Dispatcher.work_queue.
Constructor Details
#initialize ⇒ Worker
Creates a new worker thread that loops over the work queue.
:api: private
60 61 62 63 64 65 66 67 |
# File 'lib/merb-core/dispatch/worker.rb', line 60 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
#thread ⇒ Object
:api: private
5 6 7 |
# File 'lib/merb-core/dispatch/worker.rb', line 5 def thread @thread end |
Class Method Details
.alive? ⇒ Boolean
Returns
Whether the Merb::Worker instance thread is alive
:api: private
37 38 39 |
# File 'lib/merb-core/dispatch/worker.rb', line 37 def alive? started? and @worker.thread.alive? end |
.restart ⇒ Object
restarts the worker thread
Returns
- Merb::Worker
-
instance of a worker.
:api: private
47 48 49 50 51 52 53 54 |
# File 'lib/merb-core/dispatch/worker.rb', line 47 def restart # if we have a worker or thread, kill it. if started? @worker.thread.exit @worker = nil end start end |
.start ⇒ Object
Returns
- Merb::Worker
-
instance of a worker.
:api: private
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/merb-core/dispatch/worker.rb', line 12 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
Returns
Whether the Merb::Worker instance is already started.
:api: private
29 30 31 |
# File 'lib/merb-core/dispatch/worker.rb', line 29 def started? !@worker.nil? end |
Instance Method Details
#process_queue ⇒ Object
Processes tasks in the Merb::Dispatcher.work_queue.
:api: private
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/merb-core/dispatch/worker.rb', line 72 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 |