class Workers::Worker
Attributes
exception[RW]
on_exception[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/workers/worker.rb, line 8 def initialize(options = {}) @logger = Workers::LogProxy.new(options[:logger]) @input_queue = options[:input_queue] || Queue.new @on_exception = options[:on_exception] @run = true @thread = Thread.new { start_event_loop } nil end
Public Instance Methods
alive?()
click to toggle source
# File lib/workers/worker.rb, line 55 def alive? @thread && @thread.alive? end
dispose(max_wait = nil)
click to toggle source
# File lib/workers/worker.rb, line 50 def dispose(max_wait = nil) shutdown join(max_wait) end
enqueue(command, data = nil)
click to toggle source
# File lib/workers/worker.rb, line 18 def enqueue(command, data = nil) @input_queue.push(Event.new(command, data)) nil end
inspect()
click to toggle source
# File lib/workers/worker.rb, line 59 def inspect "#<#{self.class.to_s}:0x#{(object_id << 1).to_s(16)} #{alive? ? 'alive' : 'dead'}>" end
join(max_wait = nil)
click to toggle source
# File lib/workers/worker.rb, line 42 def join(max_wait = nil) raise Workers::JoinError, "Worker can't join itself." if @thread == Thread.current return true if !@thread.join(max_wait).nil? @thread.kill and return false end
kill()
click to toggle source
# File lib/workers/worker.rb, line 36 def kill @thread.kill nil end
perform(&block)
click to toggle source
# File lib/workers/worker.rb, line 24 def perform(&block) enqueue(:perform, block) nil end
shutdown(&block)
click to toggle source
# File lib/workers/worker.rb, line 30 def shutdown(&block) enqueue(:shutdown, block) nil end
Private Instance Methods
event_handler(event)
click to toggle source
Override this method to handle custom events. Make sure you call super(event) if want to built-in events to work.
# File lib/workers/worker.rb, line 80 def event_handler(event) case event.command when :shutdown shutdown_handler(event) @run = false when :perform perform_handler(event) else raise Workers::UnknownEventError, "Unhandled event (#{event.inspect})." end nil end
exception_handler(e)
click to toggle source
# File lib/workers/worker.rb, line 106 def exception_handler(e) @exception = e begin @on_exception.call(e) if @on_exception rescue Exception => e2 STDERR.puts "Your #{self.class.to_s} exception handler raised an error. Fix your handler!\n#{e2.class.to_s}: #{e2.message}\n#{e2.backtrace.join("\n")}" end nil end
perform_handler(event)
click to toggle source
# File lib/workers/worker.rb, line 100 def perform_handler(event) event.data.call nil end
process_event()
click to toggle source
# File lib/workers/worker.rb, line 71 def process_event event = @input_queue.pop event_handler(event) rescue Exception => e exception_handler(e) end
shutdown_handler(event)
click to toggle source
# File lib/workers/worker.rb, line 94 def shutdown_handler(event) event.data.call(self) if event.data nil end
start_event_loop()
click to toggle source
# File lib/workers/worker.rb, line 65 def start_event_loop while @run process_event end end