Sha256: a5f61e5f42e628ed3e6a14e5ddb3e3a1318b4fae3fc20221319fd92f4e023d00

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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

12 entries across 12 versions & 1 rubygems

Version Path
msgr-0.15.2.1.b175 lib/msgr/binding.rb
msgr-0.15.2.1.b169 lib/msgr/binding.rb
msgr-0.15.2.1.b167 lib/msgr/binding.rb
msgr-0.15.2.1.b166 lib/msgr/binding.rb
msgr-0.15.2.1.b165 lib/msgr/binding.rb
msgr-0.15.2.1.b164 lib/msgr/binding.rb
msgr-0.15.2.1.b163 lib/msgr/binding.rb
msgr-0.15.2.1.b162 lib/msgr/binding.rb
msgr-0.15.2.1.b161 lib/msgr/binding.rb
msgr-0.15.2.1.b160 lib/msgr/binding.rb
msgr-0.15.2.1.b159 lib/msgr/binding.rb
msgr-0.15.2.1.b158 lib/msgr/binding.rb