Sha256: e8d28c0bea9dea51abf3fa21e54b68aca81c0318912a5b94fe50bd5b9a0cf54f

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'bunny'

module CottonTail
  module Queue
    # A wrapper around a ::Bunny::Queue that makes it interchangeable with a
    # standard Ruby Queue
    class Bunny < SimpleDelegator
      extend Forwardable

      def self.call(**opts)
        new(**opts)
      end

      def initialize(name:, connection:, manual_ack: false, **opts)
        super ::Queue.new

        @name = name
        @source_opts = opts
        @connection = connection

        watch_source manual_ack
      end

      def push(args)
        routing_key, message = args
        bind routing_key
        exchange.publish message, routing_key: routing_key
      end

      def pop
        delivery_info, *tail = super
        [delivery_info[:routing_key], delivery_info, *tail]
      end

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

      private

      attr_reader :connection

      def watch_source(manual_ack)
        source.subscribe(manual_ack: manual_ack) { |*args| self << args }
      end

      def source
        @source ||= channel.queue(@name, **@source_opts)
      end

      def channel
        @channel ||= connection.create_channel
      end

      def exchange
        @exchange ||= channel.exchange('amq.topic')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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