Class: Push::Backend::AMQP

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/push/backend.rb

Instance Method Summary collapse

Instance Method Details

#async_publish(message, exchange) ⇒ Object

Run this puppy inside of Em.



21
22
23
24
25
26
27
28
# File 'lib/push/backend.rb', line 21

def async_publish(message, exchange)
  channel = ::AMQP::Channel.new(connection)
  channel.fanout(exchange).publish(message)
  EM.next_tick {
    channel.close
    logger.debug "Channel closed"
  }
end

#connectionObject

Make sure we setup and use one connection per Push instance. AMQP wants to minimize connects/reconnects, so we connect once here and let publish use this connection.



33
34
35
# File 'lib/push/backend.rb', line 33

def connection
  @connection ||= ::AMQP.connect(Push.config.amqp.to_hash)
end

#publish(message, exchange) ⇒ Object



7
8
9
10
# File 'lib/push/backend.rb', line 7

def publish(message, exchange)
  EM.reactor_running? ? async_publish(message, exchange) : sync_publish(message, exchange)
  logger.debug "Published '#{message}' to exchange #{exchange}"
end

#sync_publish(message, exchange) ⇒ Object

Deal with running AMQP inside of a sync environment. This is useful for script/console testing and our pe_result_processor.



14
15
16
17
18
# File 'lib/push/backend.rb', line 14

def sync_publish(message, exchange)
  Bunny.run(Push.config.amqp.to_hash) { |channel| 
    channel.exchange(exchange, :type => :fanout).publish(message)
  }
end