module Warren

Module Warren provides connection pooling for RabbitMQ Connections

Constants

VERSION

Gem version number. Bump prior to release of new version

WARREN_TYPE

Environmental variables

Public Class Methods

construct(type:, config: {}) click to toggle source

Construct a {Warren::Handler::Base} of the type `type`. For Rails apps this is usually handled automatically by the initializer.

@param type ['test','log','broadcast'] The type of warren handler to construct @param config [Hash] A configuration hash object @option config [Hash] :server Bunny connection parameters

http://rubybunny.info/articles/connecting.html#using_a_map_of_parameters

@option config [String] :exchange The default exchange to receive published messaged @option config [String] :routing_key_prefix A prefix to apply to all routing keys (Such as the environment)

@return [Warren::Handler::Base] Exact class determined by the type passed in

# File lib/warren.rb, line 32
def self.construct(type:, config: {})
  warren_type = ENV.fetch(WARREN_TYPE, type)
  case warren_type
  when 'test' then Warren::Handler::Test.new
  when 'log' then Warren::Handler::Log.new(logger: config.fetch(:logger) { Rails.logger })
  when 'broadcast' then Warren::Handler::Broadcast.new(**config)
  else raise StandardError, "Unknown type warren: #{warren_type}"
  end
end
handler() click to toggle source

Returns the global Warren handler

@return [Warren::Handler::Base] A warren handler for broadcasting messages

# File lib/warren.rb, line 53
def self.handler
  @handler
end
load_configuration() click to toggle source

When we invoke the warren consumer, we end up loading warren before rails is loaded, so don't invoke the railtie, and don't get a change to do so until after the Rails has initialized, and thus run its ties. I'm sure there is a proper way of handling this, but want to move on for now.

# File lib/warren.rb, line 61
  def self.load_configuration
    config = begin
      Rails.application.config_for(:warren)
    rescue RuntimeError => e
      warn <<~WARN
        🐇 WARREN CONFIGURATION ERROR
        #{e.message}
        Use `warren config` to generate a basic configuration file
      WARN
      exit 1
    end
    Warren.setup(config.deep_symbolize_keys.slice(:type, :config))
  end
setup(opts, logger: Rails.logger) click to toggle source

Constructs a Warren::Handler of the specified type and sets it as the global handler.

# File lib/warren.rb, line 43
def self.setup(opts, logger: Rails.logger)
  logger.warn 'Recreating Warren handler when one already exists' if handler.present?
  @handler = construct(**opts.symbolize_keys)
end