Sha256: c6f361861bef4adfe4c863c2aeb1b4bbc975edb38ea1cc87cd7355c10785cb7c

Contents?: true

Size: 997 Bytes

Versions: 11

Compression:

Stored size: 997 Bytes

Contents

module Outboxable
  module RabbitMq
    class Publisher
      def initialize(resource:)
        @resource = resource
      end

      def to_envelope(resource:)
        # throw not implemented method error
        raise NotImplementedError, 'Please implement the to_envelope method in your own module'
      end

      def publish
        confirmed = nil

        Outboxable::Connection.instance.channel.with do |channel|
          channel.confirm_select

          # Declare a exchange
          exchange = channel.topic(@resource.exchange, durable: true)

          # Publish the CloudEvent resource to the exchange
          exchange.publish(to_envelope(resource: @resource), routing_key: @resource.routing_key, headers: @resource.try(:headers) || {})

          # Wait for confirmation
          confirmed = channel.wait_for_confirms
        end

        return unless confirmed

        @resource.reload
        @resource.update(status: :published, retry_at: nil)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
outboxable-1.0.6 lib/outboxable/rabbitmq/publisher.rb
outboxable-1.0.5 lib/outboxable/rabbitmq/publisher.rb
outboxable-1.0.4 lib/outboxable/rabbitmq/publisher.rb
outboxable-1.0.3 lib/outboxable/rabbitmq/publisher.rb
outboxable-1.0.2 lib/outboxable/rabbitmq/publisher.rb
outboxable-1.0.0 lib/outboxable/rabbitmq/publisher.rb
outboxable-0.1.8 lib/outboxable/rabbitmq/publisher.rb
outboxable-0.1.7 lib/outboxable/rabbitmq/publisher.rb
outboxable-0.1.6 lib/outboxable/rabbitmq/publisher.rb
outboxable-0.1.5 lib/outboxable/rabbitmq/publisher.rb
outboxable-0.1.4 lib/outboxable/rabbitmq/publisher.rb