Sha256: 9c066baf04b3d81ec909e70af6d3cb2f36450d5ee85670fa4562926714ccff79

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

module Sneakers
  class Publisher
    def initialize(opts = {})
      @mutex = Mutex.new
      @opts = Sneakers::CONFIG.merge(opts)
    end

    def publish(msg, options = {})
      @mutex.synchronize do
        ensure_connection! unless connected?
      end
      to_queue = options.delete(:to_queue)
      options[:routing_key] ||= to_queue
      Sneakers.logger.info {"publishing <#{msg}> to [#{options[:routing_key]}]"}
      @exchange.publish(msg, options)
    end


    attr_reader :exchange

  private
    def ensure_connection!
      # 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]
      @bunny ||= create_bunny_connection
      @bunny.start
      @channel = @bunny.create_channel
      @exchange = @channel.exchange(@opts[:exchange], @opts[:exchange_options])
    end

    def connected?
      @bunny && @bunny.connected?
    end

    def create_bunny_connection
      Bunny.new(@opts[:amqp], :vhost => @opts[:vhost], :heartbeat => @opts[:heartbeat], :logger => Sneakers::logger)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sneakers-2.5.0 lib/sneakers/publisher.rb
sneakers-2.4.0 lib/sneakers/publisher.rb
sneakers-2.3.5 lib/sneakers/publisher.rb
sneakers-2.3.4 lib/sneakers/publisher.rb
sneakers-2.3.2 lib/sneakers/publisher.rb
sneakers-2.3.1 lib/sneakers/publisher.rb
sneakers-2.3.0 lib/sneakers/publisher.rb