Sha256: 533d548b4e3be13280b24fa4060377dd25d18068026e7d5afc74241e7ca31faf

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module CottonTail
  module Queue
    # A wrapper around a ::Bunny::Queue that makes it interchangeable with a
    # standard Ruby::Queue
    class Bunny
      def self.call(name:, **opts)
        new(name, **opts)
      end

      def initialize(name, conn: Connection.new, prefetch: 1, manual_ack: false, **opts)
        @name = name
        @prefetch = prefetch
        @manual_ack = manual_ack
        @conn = conn
        @closed = false
        @queue = conn.queue(@name, **opts)
      end

      def bind(routing_key)
        @queue.bind('amq.topic', routing_key: routing_key)
      end

      def push(args)
        routing_key, message = args
        @conn.publish message, routing_key: routing_key
      end

      def close
        @closed = true
      end

      def closed?
        @closed
      end

      def empty?
        @queue.message_count.zero?
      end

      def pop
        return if empty?

        delivery_info, *tail = @queue.pop(manual_ack: @manual_ack)
        [delivery_info[:routing_key], delivery_info, *tail, conn: @conn]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cotton-tail-0.1.1 lib/cotton_tail/queue/bunny.rb