Class: JobQueue

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

Overview

JobQueue abstracts the task of adding work to a queue.

Beanstalk is fantastic, but maybe not “enterprise grade”.

AMQP is fantastic, but it’s bloody complex and has to run inside an eventmachine loop.

Take your pick!

Before use, an adapter must be chosen:

JobQueue.adapter = JobQueue::BeanstalkAdapter.new

Jobs can then be simply added to the queue with

JobQueue.put("flubble bubble")

Defined Under Namespace

Classes: AMQPAdapter, ArgumentError, BeanstalkAdapter, NoConnectionAvailable, TestAdapter, VerboseAdapter

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



20
21
22
# File 'lib/job_queue/job_queue.rb', line 20

def adapter
  @adapter
end

.loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/job_queue/job_queue.rb', line 21

def logger
  @logger
end

Class Method Details

.job_stats(job_id) ⇒ Object

Returns a hash of info (exact details dependent on adapter)



58
59
60
# File 'lib/job_queue/job_queue.rb', line 58

def self.job_stats(job_id)
  adapter.job_stats(job_id)
end

.put(string, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/job_queue/job_queue.rb', line 33

def self.put(string, options = {})
  queue = options[:queue] || 'default'
  priority = options[:priority] || 50
  ttr = options[:ttr] || 60
  adapter.put(string, queue, priority, ttr)
end

.queue_length(queue = nil) ⇒ Object



62
63
64
# File 'lib/job_queue/job_queue.rb', line 62

def self.queue_length(queue = nil)
  adapter.queue_length(queue)
end

.subscribe(options = {}, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/job_queue/job_queue.rb', line 40

def self.subscribe(options = {}, &block)
  queue = options[:queue] || 'default'
  error_report = options[:error_report] || Proc.new do |job_body, e|
    JobQueue.logger.error \
      "Job failed\n" \
      "==========\n" \
      "Job content: #{job_body.inspect}\n" \
      "Exception: #{e.message}\n" \
      "#{e.backtrace.join("\n")}\n" \
      "\n"
  end
  cleanup_task = options[:cleanup] || lambda {}
  catch :stop do
    adapter.subscribe(error_report, cleanup_task, queue, &block)
  end
end