Sha256: 9dd9e132977e718fbb97e3a121fa3ca0f3c20a7d6961a971c1bb19fa45eb9167

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Refinery #:nodoc:
  # Mix this module in to classes that want to access a queue.
  module Queueable
    include Loggable
    include Configurable
    # Get a named queue
    def queue(name)
      queue_provider.queue(name)
    end
    
    # Given the queue name and a block, yield the named queue into 
    # the block. This method handles any exceptions that are raised
    # in the block and will recreate the provider automatically.
    #
    # Note that errors will not be propagated beyond this block. You
    # have been warned.
    def with_queue(name, &block)
      begin
        yield queue(name)
      rescue Exception => e
        logger.error "Queue error: #{e.message}"
        # this removes the sqs connection from the current thread.
        # note that this is brittle and will break if the RightAWS 
        # library changes the name or the way the sqs connection is
        # stored in the thread local hash
        Thread.current[:sqs_connection] = nil
        sleep(5)
        retry
      end
    end
    
    protected
    # Get the queue provider. Defaults to RightAws::SqsGen2 running
    # in multi-thread mode.
    def queue_provider
      @queue_provider ||= RightAws::SqsGen2.new(
        config['aws']['credentials']["access_key_id"], 
        config['aws']['credentials']["secret_access_key"],
        {:multi_thread => true}
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
aeden-refinery-0.9.15 lib/refinery/queueable.rb
refinery-0.9.15 lib/refinery/queueable.rb