Sha256: bd142ec0e5bb1c121465ab1c42a75770cbba0c5f3e43b0048ebff2b64f2c2982

Contents?: true

Size: 904 Bytes

Versions: 2

Compression:

Stored size: 904 Bytes

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!
      @bunny = Bunny.new(@opts[:amqp], heartbeat: @opts[:heartbeat], vhost: @opts[:vhost], :logger => Sneakers::logger)
      @bunny.start
      @channel = @bunny.create_channel
      @exchange = @channel.exchange(@opts[:exchange], type: @opts[:exchange_type], durable: @opts[:durable])
    end

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

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
sneakers_custom_bunny-1.0.4 lib/sneakers/publisher.rb
sneakers-1.0.4 lib/sneakers/publisher.rb