module RabidMQ::Publisher

Public Class Methods

amqp_broadcast(topic, payload, routing_key: self.default_amqp_routing_key, include_environment_name: true, durable: false, **options) click to toggle source
# File lib/rabid_mq/publisher.rb, line 26
def amqp_broadcast(topic, payload, routing_key: self.default_amqp_routing_key, include_environment_name: true, durable: false, **options)
  exchange = topic_exchange(topic, include_environment_name: include_environment_name, durable: durable)
  exchange.publish(payload, routing_key: routing_key, **options)
rescue  => e
  if defined? ::Rails
    Rails.logger.error e.message
  else
    puts e.message
  end
end
amqp_connect() click to toggle source

Start a new connection

# File lib/rabid_mq/publisher.rb, line 70
def amqp_connect
  connection.tap do |c|
    c.start
  end
end
channel() click to toggle source
# File lib/rabid_mq/publisher.rb, line 57
def channel
  return @channel if @channel && !@channel.closed?
  @channel = amqp_connect.create_channel
rescue Bunny::ChannelAlreadyClosed => e
  reconnect
end
connection() click to toggle source

Provide a new or existing Bunny::Session

# File lib/rabid_mq/publisher.rb, line 83
def connection
  @connection ||= Bunny.new RabidMQ::Config.load_config
end
default_amqp_routing_key() click to toggle source
# File lib/rabid_mq/publisher.rb, line 41
def default_amqp_routing_key
  self.name.underscore.gsub(/\//, '.')
end
fanout_exchange(topic, include_environment_name: true, **options) click to toggle source

Provide fanout exchange

# File lib/rabid_mq/publisher.rb, line 52
def fanout_exchange(topic, include_environment_name: true, **options)
  topic_name = include_environment_name ? name_with_env(topic) : topic
  channel.fanout(topic_name, **options)
end
name_with_env(name) click to toggle source
# File lib/rabid_mq/publisher.rb, line 76
def name_with_env(name)
  return name unless defined?(::Rails)
  return name if name.match /\[(development|test|production|integration|pod)\]/
  name + "[#{Config.environment}]"
end
reconnect() click to toggle source
# File lib/rabid_mq/publisher.rb, line 64
def reconnect
  @channel = nil
  channel
end
topic_exchange(topic, include_environment_name: true, **options) click to toggle source

Provide a topic exchange on demand connected to the existing channel

# File lib/rabid_mq/publisher.rb, line 46
def topic_exchange(topic, include_environment_name: true, **options)
  topic_name = include_environment_name ? name_with_env(topic) : topic
  channel.topic(topic_name, **options)
end

Public Instance Methods

amqp_broadcast(*args, **options) click to toggle source
# File lib/rabid_mq/publisher.rb, line 17
def amqp_broadcast(*args, **options)
  self.class.amqp_broadcast *args, **options
end