Class: JobQueue::TestAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/job_queue/adapters/test_adapter.rb

Overview

This adapter is designed for testing purposes.

Features supported:

named queues: yes priority: no ttr: no

Additionally this queue can be inspeced with JobQueue.adapter.queue(‘name’)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TestAdapter

Returns a new instance of TestAdapter.



12
13
14
# File 'lib/job_queue/adapters/test_adapter.rb', line 12

def initialize(options = {})
  @queues = {}
end

Instance Method Details

#put(string, queue, priority, ttr) ⇒ Object



16
17
18
19
# File 'lib/job_queue/adapters/test_adapter.rb', line 16

def put(string, queue, priority, ttr)
  @queues[queue] ||= []
  @queues[queue] << string
end

#queue(queue = 'default') ⇒ Object

Additional method for TestAdapter to allow easy queue inspection with

JobQueue.adapter.queue(‘foo’)



40
41
42
# File 'lib/job_queue/adapters/test_adapter.rb', line 40

def queue(queue = 'default')
  get_queue(queue)
end

#subscribe(error_report, cleanup_task, queue, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/job_queue/adapters/test_adapter.rb', line 21

def subscribe(error_report, cleanup_task, queue, &block)
  loop do
    begin
      if get_queue(queue).empty?
        sleep 0.1
      else
        job = get_queue(queue).shift
        yield job
      end
    rescue => e
      error_report.call(job, e)
    end
  end
end