class Bixby::ThreadPool::Worker
Attributes
thread[R]
Public Class Methods
new(queue, idle_timeout, exit_handler)
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 12 def initialize(queue, idle_timeout, exit_handler) @input_queue = queue @idle_timeout = idle_timeout @exit_handler = exit_handler @running = true @working = false @thread = Thread.new { start_run_loop } end
Public Instance Methods
alive?()
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 29 def alive? return @running && @thread && @thread.alive? end
inspect()
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 37 def inspect return "#<#{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/bixby-common/util/thread_pool/worker.rb, line 21 def join(max_wait = nil) raise "Worker can't join itself." if @thread == Thread.current return true if @thread.nil? || !@thread.join(max_wait).nil? @thread.kill and return false end
to_s()
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 41 def to_s inspect end
working?()
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 33 def working? @working end
Private Instance Methods
start_run_loop()
click to toggle source
# File lib/bixby-common/util/thread_pool/worker.rb, line 48 def start_run_loop begin while true task = nil Timeout::timeout(@idle_timeout) { task = @input_queue.pop } case task.command when :shutdown # logger.debug "#{inspect} thread shutting down" task.block.call(self) if task.block @running = false @thread = nil return nil when :perform @working = true # logger.debug "#{inspect} got work" task.block.call if task.block @working = false end end rescue Timeout::Error => ex if @exit_handler.call(self, :timeout) then # true result means we should exit # logger.debug "worker exiting due to idle timeout (#{@idle_timeout} sec)" @running = false return end rescue Exception => e @running = false logger.error "Worker runloop died: #{e.message}\n" + e.backtrace.join("\n") @exit_handler.call(self, :exception) end end