class Sneakers::Publisher

Attributes

channel[R]
exchange[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/sneakers/publisher.rb, line 6
def initialize(opts = {})
  @mutex = Mutex.new
  @opts = Sneakers::CONFIG.merge(opts)
  # If we've already got a bunny object, use it.  This allows people to
  # specify all kinds of options we don't need to know about (e.g. for ssl).
  @bunny = @opts[:connection]
end

Public Instance Methods

ensure_connection!() click to toggle source
# File lib/sneakers/publisher.rb, line 22
def ensure_connection!
  @mutex.synchronize do
    connect! unless connected?
  end
end
publish(msg, options = {}) click to toggle source
# File lib/sneakers/publisher.rb, line 14
def publish(msg, options = {})
  ensure_connection!
  to_queue = options.delete(:to_queue)
  options[:routing_key] ||= to_queue
  Sneakers.logger.info {"publishing <#{msg}> to [#{options[:routing_key]}]"}
  @exchange.publish(ContentType.serialize(msg, options[:content_type]), options)
end

Private Instance Methods

connect!() click to toggle source
# File lib/sneakers/publisher.rb, line 29
def connect!
  @bunny ||= create_bunny_connection
  @bunny.start
  @channel = @bunny.create_channel
  @exchange = @channel.exchange(@opts[:exchange], @opts[:exchange_options])
end
connected?() click to toggle source
# File lib/sneakers/publisher.rb, line 36
def connected?
  @bunny && @bunny.connected? && channel
end
create_bunny_connection() click to toggle source
# File lib/sneakers/publisher.rb, line 40
def create_bunny_connection
  Bunny.new(@opts[:amqp], :vhost => @opts[:vhost],
                          :heartbeat => @opts[:heartbeat],
                          :properties => @opts.fetch(:properties, {}),
                          :logger => Sneakers::logger)
end