Sha256: b220607024b68bbc5f60a7547999e6db6ba3e1b069e42f7f3e320c37c22acf85

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Msgr

  class Binding
    include Logging

    attr_reader :queue, :subscription, :connection, :route, :dispatcher

    def initialize(connection, route, dispatcher)
      @connection = connection
      @route      = route
      @dispatcher = dispatcher
      @queue      = connection.queue(route.name)

      route.keys.each do |key|
        log(:debug) { "Bind #{key} to #{queue.name}." }

        queue.bind connection.exchange, routing_key: key
      end

      subscribe
    end

    def release
      subscription.cancel
    end

    def delete
      release
      queue.delete
    end

    def purge(release: true)
      self.release if release

      queue.purge

      subscribe if release
    end

    private

    def subscribe
      @subscription = queue.subscribe(manual_ack: true) do |*args|
        begin
          dispatcher.call Message.new(connection, *args, route)
        rescue => err
          log(:error) do
            "Rescued error from subscribe: #{err.class.name}: #{err}\n#{err.backtrace.join("\n")}"
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
msgr-0.14.1.1.b125 lib/msgr/binding.rb
msgr-0.14.1.1.b124 lib/msgr/binding.rb
msgr-0.14.1.1.b112 lib/msgr/binding.rb