class Reliable::Worker

Public Class Methods

new(queue, &block) click to toggle source
# File lib/reliable/worker.rb, line 10
def initialize(queue, &block)
  @queue = queue
  @block = block
  @processing_key = "#{queue.base_key}:workers:#{SecureRandom.uuid}:processing"
  @redis = Redis.new
end

Public Instance Methods

logger() click to toggle source
# File lib/reliable/worker.rb, line 51
def logger
  Reliable.logger
end
next() click to toggle source
# File lib/reliable/worker.rb, line 21
def next
  uuid = @redis.brpoplpush pending.key, processing.key
  return if uuid.nil?

  item = @redis.get uuid

  if item
    catch(:failed) do
      process item, &@block
      @redis.lpop_and_del processing.key, uuid
      logger.info "Processed #{uuid}"
    end
  end

  item
rescue StandardError => e
  @redis.rpoplpush processing.key, failed.key
  notify(e, uuid: uuid, worker: processing.key)
  nil
end
notify(e, other = {}) click to toggle source
# File lib/reliable/worker.rb, line 55
def notify(e, other = {})
  # TODO: make configurable
  logger.info e.inspect
  logger.info e.backtrace
  logger.info other.inspect
end
processing() click to toggle source
# File lib/reliable/worker.rb, line 17
def processing
  @processing ||= List.new(@processing_key, @redis)
end

Private Instance Methods

process(item, &block) click to toggle source
# File lib/reliable/worker.rb, line 42
def process(item, &block)
  block.yield item
rescue StandardError => e
  @redis.rpoplpush processing.key, failed.key
  notify(e, worker: processing.key)
  throw :failed
end