Class: Artsy::EventPublisher::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/artsy/event_publisher.rb

Constant Summary collapse

OPTIONS =
{
  persistent: true,
  content_type: "application/json",
  headers: {}
}

Class Method Summary collapse

Class Method Details

.build_connectionObject



74
75
76
# File 'lib/artsy/event_publisher.rb', line 74

def self.build_connection
  Bunny.new(Config.rabbitmq_url).tap(&:start)
end

.get_connectionObject

Synchronized access to the connection



66
67
68
69
70
71
72
# File 'lib/artsy/event_publisher.rb', line 66

def self.get_connection
  @mutex.synchronize do
    @connection ||= build_connection
    @connection = build_connection if @connection.closed?
    @connection
  end
end

.publish(topic:, routing_key:, payload:) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/artsy/event_publisher.rb', line 49

def self.publish(topic:, routing_key:, payload:)
  with_channel do |channel|
    options = OPTIONS.merge(routing_key: routing_key, app_id: Config.app_id)
    channel.topic(topic, durable: true).publish(payload, options)
    raise Error, "Publishing failed" unless channel.wait_for_confirms
  end
end

.with_channelObject



57
58
59
60
61
62
63
# File 'lib/artsy/event_publisher.rb', line 57

def self.with_channel
  channel = get_connection.create_channel
  channel.confirm_select
  yield channel if block_given?
ensure
  channel.close if channel&.open?
end