Sha256: 32d0dcae26f3e72f96852c78e9b0ad74ab0c8b89e84f78d0a5148f0774b7bbf2

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module Msgr
  # A single binding
  class Binding
    include Logging
    attr_reader :connection, :route, :subscription, :dispatcher, :queue

    def initialize(connection, route, dispatcher)
      @connection = connection
      @route      = route
      @dispatcher = dispatcher

      exchange  = connection.exchange
      @queue    = connection.queue route.name

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

        queue.bind exchange, routing_key: key
      end

      @subscription = queue.subscribe(ack: true) { |*args| call *args }
    end

    # Called from Bunny Thread Pool. Will create message object from
    # provided bunny data and dispatch message to connection.
    #
    def call(info, metadata, payload)
      dispatcher.dispatch Message.new(connection, info, metadata, payload, route)
    rescue => error
      log(:error) { "Error received within subscribe handler: #{error.inspect}\n#{error.backtrace.join("\n  ")}" }
    end

    # Cancel subscription to not receive any more messages.
    #
    def release
      subscription.cancel if subscription
    end

    def release_if_empty
      if queue_empty?
        release
        true
      else
        false
      end
    end

    def queue_empty?
      @queue.message_count == 0
    end

    def delete
      release
      queue.delete
    end

    def purge
      queue.purge
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
msgr-0.4.1 lib/msgr/binding.rb
msgr-0.4.0 lib/msgr/binding.rb
msgr-0.3.0 lib/msgr/binding.rb
msgr-0.2.1 lib/msgr/binding.rb
msgr-0.2.0 lib/msgr/binding.rb
msgr-0.1.1 lib/msgr/binding.rb
msgr-0.1.0 lib/msgr/binding.rb