Class: QueueLite::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/queue_lite/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ Worker

Returns a new instance of Worker.



7
8
9
10
# File 'lib/queue_lite/worker.rb', line 7

def initialize(queue)
  @queue = queue
  @running = false
end

Instance Method Details

#perform_onceObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/queue_lite/worker.rb', line 19

def perform_once
  task = queue.pop
  return if task.nil?

  klass, *args = JSON.parse(task.data)
  begin
    Object.const_get(klass).perform(*args).tap do
      queue.done(task.id)
    end
  rescue
    queue.failed(task.id)
  end
end

#runObject



12
13
14
15
16
17
# File 'lib/queue_lite/worker.rb', line 12

def run
  @running = true
  while @running
    perform_once
  end
end

#shutdownObject



33
34
35
# File 'lib/queue_lite/worker.rb', line 33

def shutdown
  @running = false
end