File: rabbitmq_bus.rb

package info (click to toggle)
open-build-service 2.9.4-10
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 30,544 kB
  • sloc: ruby: 73,389; perl: 51,999; xml: 9,782; sh: 5,540; javascript: 2,743; sql: 1,232; python: 435; makefile: 229; cpp: 46
file content (22 lines) | stat: -rw-r--r-- 779 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class RabbitmqBus
  def self.publish(event_routing_key, event_payload)
    return unless CONFIG['amqp_options']
    start_connection

    $rabbitmq_exchange.publish(event_payload, routing_key: event_routing_key)
  end

  # Start one connection, channel and exchange per rails process
  # and reuse them
  def self.start_connection
    $rabbitmq_conn ||= Bunny.new(CONFIG['amqp_options'].try(:symbolize_keys))
    $rabbitmq_conn.start
    $rabbitmq_channel ||= $rabbitmq_conn.create_channel
    $rabbitmq_exchange = if CONFIG['amqp_exchange_name']
      $rabbitmq_channel.exchange(CONFIG['amqp_exchange_name'], CONFIG['amqp_exchange_options'].try(:symbolize_keys) || {})
    else
      $rabbitmq_channel.default_exchange
    end
  end
  private_class_method :start_connection
end