Sha256: 5821b53e72468754865d7a8710c5ba83e86bd6d6ac17cc107a3f5ec164489801

Contents?: true

Size: 1.11 KB

Versions: 24

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

24 entries across 24 versions & 1 rubygems

Version Path
msgr-0.15.2.1.b157 lib/msgr/binding.rb
msgr-0.15.2.1.b156 lib/msgr/binding.rb
msgr-0.15.2.1.b155 lib/msgr/binding.rb
msgr-0.15.2.1.b154 lib/msgr/binding.rb
msgr-0.15.2.1.b152 lib/msgr/binding.rb
msgr-0.15.1.1.b151 lib/msgr/binding.rb
msgr-0.15.2 lib/msgr/binding.rb
msgr-0.15.1.1.b150 lib/msgr/binding.rb
msgr-0.15.1.1.b146 lib/msgr/binding.rb
msgr-0.15.1.1.b145 lib/msgr/binding.rb
msgr-0.15.1.1.b144 lib/msgr/binding.rb
msgr-0.15.1.1.b143 lib/msgr/binding.rb
msgr-0.15.1.1.b141 lib/msgr/binding.rb
msgr-0.15.0.1.b140 lib/msgr/binding.rb
msgr-0.15.1 lib/msgr/binding.rb
msgr-0.15.0.1.b139 lib/msgr/binding.rb
msgr-0.15.0.1.b136 lib/msgr/binding.rb
msgr-0.15.0.1.b135 lib/msgr/binding.rb
msgr-0.15.0.1.b134 lib/msgr/binding.rb
msgr-0.15.0.1.b131 lib/msgr/binding.rb