Skip to content

Instantly share code, notes, and snippets.

@dakatsuka
Created May 14, 2012 09:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dakatsuka/2692861 to your computer and use it in GitHub Desktop.
Save dakatsuka/2692861 to your computer and use it in GitHub Desktop.
Rails3 + unicorn + RabbitMQ
# 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"]
development:
uri: "amqp://localhost"
# 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
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