class Quebert::Worker

Attributes

backend[RW]
exception_handler[RW]
queues[RW]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/quebert/worker.rb, line 7
def initialize
  @queues = []
  yield self if block_given?
end

Public Instance Methods

safe_stop() click to toggle source
# File lib/quebert/worker.rb, line 42
def safe_stop
  if @terminate_sent
    logger.info "Ok! I get the point. Shutting down immediately."
    stop
  else
    logger.info "Finishing current job then shutting down."
    @terminate_sent = true
    stop unless @controller
  end
end
start() click to toggle source

Start the worker backend and intercept exceptions if a handler is provided

# File lib/quebert/worker.rb, line 13
def start
  Signal.trap('TERM') { safe_stop }
  Signal.trap('INT') { safe_stop }

  logger.info "Worker started with #{backend.class.name} backend\n"

  backend.queues = queues if backend.respond_to?(:queues=)

  while @controller = backend.reserve do
    begin
      @controller.perform
    rescue => error
      if exception_handler
        exception_handler.call(
          error,
          :controller => @controller,
          :pid => $$,
          :worker => self
        )
      else
        raise
      end
    end
    @controller = nil

    stop if @terminate_sent
  end
end
stop() click to toggle source
# File lib/quebert/worker.rb, line 53
def stop
  logger.info "Worker stopping\n"
  exit 0
end