Created
May 14, 2012 09:02
-
-
Save dakatsuka/2692861 to your computer and use it in GitHub Desktop.
Rails3 + unicorn + RabbitMQ
This file contains hidden or 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
# config/initializers/amqp.rb | |
require 'amqp/utilities/event_loop_helper' | |
require 'amqp/integration/rails' | |
module AMQPManager | |
def self.start | |
AMQP::Utilities::EventLoopHelper.run | |
AMQP::Integration::Rails.start do |connection| | |
connection.on_error do |ch, connection_close| | |
raise connection_close.reply_text | |
end | |
connection.on_tcp_connection_loss do |conn, settings| | |
conn.reconnect(false, 2) | |
end | |
connection.on_tcp_connection_failure do |conn, settings| | |
conn.reconnect(false, 2) | |
end | |
channel = AMQP::Channel.new(connection, AMQP::Channel.next_channel_id, :auto_recovery => true) | |
channel.on_error do |ch, channel_close| | |
raise channel_close.reply_text | |
end | |
AMQP.channel = channel | |
end | |
end | |
end | |
AMQPManager.start unless ENV["UNICORN"] |
This file contains hidden or 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
development: | |
uri: "amqp://localhost" |
This file contains hidden or 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
# coding: utf-8 | |
class AmqpController < ApplicationController | |
def publish | |
AMQP::Utilities::EventLoopHelper.run do | |
AMQP.channel.default_exchange.publish("Hello World!!!!!", routing_key: "queue.name") | |
end | |
head :created | |
end | |
end |
This file contains hidden or 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
ENV["UNICORN"] = "true" | |
after_fork do |server, worker| | |
AMQPManager.start | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment