-
-
Save JustinAiken/bd184aae1d5ba7691355 to your computer and use it in GitHub Desktop.
lib/rabbit_mq.rb
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' | |
module RabbitMq | |
def self.startup_connection | |
if defined?(PhusionPassenger) | |
PhusionPassenger.on_event(:starting_worker_process) do |forked| | |
if forked && EM.reactor_running? | |
Rails.logger.info "Stopping EM..." | |
EM.stop | |
end | |
Thread.new { | |
EM.run do | |
AMQP.channel ||= AMQP::Channel.new(AMQP.connect(host: "127.0.0.1", port: 5672)) | |
Rails.logger.info "AMQP.channel connected..." | |
end | |
} | |
Rails.logger.info "AMQP Initialized!" | |
die_gracefully_on_signal | |
end | |
else | |
Rails.logger.warn "passenger not forked :(" | |
end | |
Rails.logger.info "AMQP REALLY Initialized!" | |
end | |
def self.die_gracefully_on_signal | |
Signal.trap("INT") { EM.stop } | |
Signal.trap("TERM") { EM.stop } | |
end | |
def self.send_message message | |
begin | |
channel = AMQP.channel | |
queue = channel.queue("test.queue", :auto_delete => false) | |
exchange = channel.direct("") | |
status = exchange.publish "Hello, world!", :routing_key => queue.name | |
rescue Exception => e | |
Rails.logger.warn "Error! #{e}" | |
return false | |
end | |
status.inspect | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment