unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: unicorn list <mongrel-unicorn@rubyforge.org>
Cc: Marcelo de Moraes Serpa <fullofcaffeine@gmail.com>
Subject: Re: AMQP and Unicorn (mq gem)
Date: Wed, 10 Nov 2010 08:55:06 +0800	[thread overview]
Message-ID: <20101110005506.GA25886@dcvr.yhbt.net> (raw)
In-Reply-To: <loom.20101109T184430-639@post.gmane.org>

Marcelo de Moraes Serpa <fullofcaffeine@gmail.com> wrote:
> Hi all,
> I'm having issues with Unicorn and connecting to 
> RabbitMQ using the tmm1-amqp gem.
>  I've tried lots of  approaches.
> The classic initializer with Thread.new { EM.run } 
> for the Rails app and even tried using 
> the Qusion library. 
>  (https://github.com/danielsdeleo/qusion)
> I've made a simple mod to Qusion for it to monkey patch 
> unicorn too. 
> The code looks like:
> /vendor/plugins/qusion/lib/amqp.rb
> module AMQP
>   def self.start_web_dispatcher(amqp_settings={})
>     @settings = settings.merge(amqp_settings)
>     case Qusion::ServerSpy.server_type
>     when :passenger
>       PhusionPassenger.on_event(:starting_worker_process) do |forked| 

<side note>maybe adding a similar hook to Unicorn for library/app authors
to use would be a good idea.</side note>

>         if forked
>           EM.kill_reactor
>           Thread.current[:mq], @conn = nil, nil
>         end 
>         Thread.new { start }
>         die_gracefully_on_signal
>       end
>     when :standard
>       Thread.new { start }
>       die_gracefully_on_signal
>     when :evented
>       die_gracefully_on_signal
>     when :none # << HERE
>       Thread.new { start }
>       die_gracefully_on_signal
>     else
>       raise ArgumentError, "AMQP#start_web_dispatcher
>  requires an argument of 
> [:standard|:evented|:passenger|:none]"
>     end
>   end
>   def self.die_gracefully_on_signal
>     Signal.trap("INT")  { AMQP.stop { EM.stop } }
>     Signal.trap("TERM") { AMQP.stop { EM.stop } }
>   end

Registering a standard at_exit {} block in the Unicorn
after_fork hook would be a good idea than trapping INT
and TERM, which Unicorn uses.

> end
> See the :none case? It used to be empty and return nil. 
> Now it has the same behavior as the non-
> evented :standard server. 
> The problem is that the "server_spy" wasn't 
> recognizing Unicorn. So far so 
> good.
> Originally, the AMQP/Qusion conf goes into config/environment.rb
>    config.after_initialize do
>       Qusion.start(:user => 'guest',:pass => 'mypass') 
>  end
> However, when I publish messages to the queue,
>  it just isn't posted. It silently fails. 
> I've then tried to put this on config/unicorn.rb:
> before_fork do |server,worker|
>  Qusion.start(:user => 'guest',:pass => 'mypass')
>  MQ.new.queue('jobs').publish('hey!')
> end
> And the hey! message is posted. 
> However, it only works from here, the MQ.new.queue on the app code 
> fails silently.
> Any ideas on what might be happenig?

Is preload_app true?  Then you're probably trying to share sockets
across processes and confusing EM or AMQP.

Instead of using before_fork above, you should probably
be using after_fork:

  before_fork do |server,worker|
    Qusion.start(:user => 'guest',:pass => 'mypass')

I'm not very familiar with AMQP, but the Unicorn process is strictly an
AMQP sender/client and doesn't listen as a server on a port, right?
If so, the above before_fork config *should* work for you.

Let us know how it goes!

Eric Wong
Unicorn mailing list - mongrel-unicorn@rubyforge.org
Do not quote signatures (like this one) or top post when replying

  reply	other threads:[~2010-11-10  1:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-09 17:57 AMQP and Unicorn (mq gem) Marcelo de Moraes Serpa
2010-11-10  0:55 ` Eric Wong [this message]
2010-11-10  2:11   ` Jordan Ritter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101110005506.GA25886@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=fullofcaffeine@gmail.com \
    --cc=mongrel-unicorn@rubyforge.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).