Class: Outboxable::RabbitMq::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/outboxable/rabbitmq/publisher.rb,
lib/templates/mongoid_initializer.rb,
lib/templates/activerecord_initializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ Publisher

Returns a new instance of Publisher.



4
5
6
# File 'lib/outboxable/rabbitmq/publisher.rb', line 4

def initialize(resource:)
  @resource = resource
end

Instance Method Details

#publishObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/outboxable/rabbitmq/publisher.rb', line 13

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

#to_envelope(resource:) ⇒ Object

Override this method to customize the message format that you publish to your broker DO NOT CHANGE THE METHOD SIGNATURE



8
9
10
11
# File 'lib/templates/mongoid_initializer.rb', line 8

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