Created
March 21, 2014 11:13
-
-
Save simonmorley/9684010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "amqp" | |
error_handler = Proc.new do |settings| | |
puts "Failed to connect." | |
EM.stop | |
end | |
Thread.new { AMQP.start( | |
:port => 5672, | |
:vhost => '/', | |
:user => 'guest', | |
:password => 'guest', | |
:timeout => 10, | |
:on_tcp_connection_failure => error_handler)} | |
class MessageQueue | |
def initialize() | |
EventMachine.next_tick do | |
AMQP.channel ||= AMQP::Channel.new(AMQP.connection) | |
$exchange = AMQP.channel.topic("polkaspots", durable: true, auto_delete: false) | |
end | |
end | |
def push(message, routing_key = '#', expiration = nil) | |
EventMachine.next_tick do | |
$exchange.publish( | |
message, | |
message_id: SecureRandom.uuid, | |
content_type: "application/json", | |
expiration: expiration, | |
routing_key: routing_key | |
) | |
end | |
end | |
alias_method :<<, :push | |
end | |
MESSAGE_QUEUE = MessageQueue.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment