Class: Reliable::Worker

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/reliable/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue, &block) ⇒ Worker

Returns a new instance of Worker.



10
11
12
13
14
15
# 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

Instance Method Details

#loggerObject



51
52
53
# File 'lib/reliable/worker.rb', line 51

def logger
  Reliable.logger
end

#nextObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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 = {}) ⇒ Object



55
56
57
58
59
60
# 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

#processingObject



17
18
19
# File 'lib/reliable/worker.rb', line 17

def processing
  @processing ||= List.new(@processing_key, @redis)
end