Sha256: 67992c93462578220080e45b4db0a3da4b47f99dec4a5a52dbbf9a7cfdeec30a

Contents?: true

Size: 840 Bytes

Versions: 4

Compression:

Stored size: 840 Bytes

Contents

# frozen_string_literal: true

module Totoro
  class SubscribeService
    def initialize(config)
      @config = config
    end

    def subscribe(id)
      queue = bind_queue(id)
      queue.purge if @config.clean_start?(id)
      queue.subscribe(manual_ack: @config.manual_ack?(id)) do |delivery_info, metadata, payload|
        yield(delivery_info, metadata, payload)
      ensure
        channel.ack(delivery_info.delivery_tag) if @config.force_ack?(id)
      end
    end

    def channel
      @channel ||= Bunny.new(@config.connect).tap(&:start).create_channel
    end

    private

    def bind_queue(id)
      exchange_name = @config.exchange_name_for_queue(id)
      if exchange_name.nil?
        channel.queue(*@config.queue(id))
      else
        channel.queue(*@config.queue(id)).bind(exchange_name)
      end
    end
  end
end

Version data entries

4 entries across 3 versions & 1 rubygems

Version Path
totoro-1.0.7 lib/totoro/services/subscribe_service.rb
totoro-1.0.6 lib/totoro/services/subscribe_service.rb
totoro-1.0.6 pkg/totoro-1.0.5/lib/totoro/services/subscribe_service.rb
totoro-1.0.5 lib/totoro/services/subscribe_service.rb