Sha256: af8b9ec6e02dd95b661cd8f26ff6cbfcd2b171607934b927857acaed9c21ad22

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# 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")
# 
class JobQueue
  class << self
    attr_accessor :adapter
    attr_accessor :logger
    
    def logger
      @logger ||= begin
        logger = Logger.new(STDOUT)
        logger.level = Logger::WARN
        logger.debug("Created logger")
        logger
      end
    end
  end
  
  def self.put(string, options = {})
    queue = options[:queue] || 'default'
    priority = options[:priority] || 50
    adapter.put(string, queue, priority)
  end
  
  def self.subscribe(options = {}, &block)
    queue = options[:queue] || 'default'
    error_report = options[:error_report] || begin
      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
    end
    catch :stop do
      adapter.subscribe(error_report, queue, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mloughran-job_queue-0.0.5 lib/job_queue/job_queue.rb