class ChiliLogger::RabbitBroker
class that configures and manages the RabbitMQ client
Public Class Methods
new(custom_config = {})
click to toggle source
# File lib/brokers/rabbit_broker.rb, line 9 def initialize(custom_config = {}) custom_config ||= {} config = rabbit_config(custom_config) @user = config[:user] @password = config[:password] @ip = config[:ip] @port = config[:port] @exchange_name = config[:exchange_name] @routing_key_overwriter = custom_config[:routing_key_overwriter] connect end
Public Instance Methods
connect()
click to toggle source
# File lib/brokers/rabbit_broker.rb, line 23 def connect return if ChiliLogger.instance.deactivated @connection = Bunny.new(connection_config) @connection.start @channel = @connection.create_channel @exchange = @channel.topic(@exchange_name, durable: true) rescue StandardError => e puts 'Could not connect to RabbitMQ due to the following error:' puts e end
publish(message)
click to toggle source
# File lib/brokers/rabbit_broker.rb, line 36 def publish(message) return if ChiliLogger.instance.deactivated # if no routing_key was provided when configuring RabbitBroker, than use the message's description tag key = @routing_key_overwriter || message[:desc] || message['desc'] @exchange.publish(message.to_json, routing_key: key) puts "sent message to #{@exchange_name}, with routing_key = '#{key}'" end
Private Instance Methods
connection_config()
click to toggle source
# File lib/brokers/rabbit_broker.rb, line 47 def connection_config { user: @user, password: @password, host: @ip, port: @port } end
rabbit_config(custom_config)
click to toggle source
# File lib/brokers/rabbit_broker.rb, line 56 def rabbit_config(custom_config) default_secrets = Values::Secrets.new.get_secrets_collection('ChiliLoggerRabbitCredentials') default_keys = %i[user password ip port exchange_name] config = {} default_keys.each do |key| config[key] = custom_config[key] || default_secrets[key.to_s] end config end