Sha256: 55f242316dda43c8ea538cf7c49baccf2a149366366e5bd4d77853d109398603

Contents?: true

Size: 979 Bytes

Versions: 4

Compression:

Stored size: 979 Bytes

Contents

module Refinery #:nodoc:
  # An interface to beanstalk using SQS-compatible methods.
  class BeanstalkQueue
    include Refinery::Loggable
    attr_reader :name
    
    # Construct a BeanstalkQueue instance.
    #
    # *<tt>name</tt>: if specified then that "tube" will be used.
    # *<tt>host</tt>: if specified then those host:port combos will be used.
    def initialize(name=nil, hosts=nil)
      @name = name.gsub(/_/, '-') # beanstalk does not like underscores in tube names
      @hosts = hosts || ['localhost:11300']
    end
    
    # Get the next message from the queue
    def receive(visibility=nil)
      beanstalk.reserve(visibility)
    end
    
    # Get the approximate queue size
    def size
      beanstalk.stats_tube(name)['current-jobs-ready']
    end
    
    # Send a message
    def send_message(message)
      beanstalk.put(message)
    end
    
    protected
    def beanstalk
      @beanstalk ||= Beanstalk::Pool.new(@hosts, name)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
refinery-0.12.2 lib/refinery/beanstalk_queue.rb
refinery-0.12.1 lib/refinery/beanstalk_queue.rb
refinery-0.12.0 lib/refinery/beanstalk_queue.rb
refinery-0.11.0 lib/refinery/beanstalk_queue.rb