gracefully shutting down AMQP when a script exists (without dropping msgs)

16 views
Skip to first unread message

Adam Greene

unread,
Nov 10, 2009, 7:35:32 PM11/10/09
to AMQP
hey everyone,

I have a script that at the end pushes a msg to the queue and then
exits (no exit called, it just hits the end of the script). I noticed
that the msg wasn't ending up on a queue unless I added a sleep(2) at
the very bottom of the script, and then it was added. removing the
sleep(2) causes the msg to be dropped.

specifically I'm using rails, and this script is part of db/seeds.rb,
and triggered by 'rake db:setup'. The very last thing it does is try
to put a msg onto an existing queue. However, it only will do so if I
add a sleep(2) at the bottom of seeds.rb. If there is no sleep before
it exists, the msg is not put on the queue.

I suspect that the msg is somewhere in EM which is exiting before the
msg can be written.

AMQP is running in its own thread, via:
Thread.new { AMQP.start(start_configs) }

and the following traps are defined:
Signal.trap('INT') { AMQP.stop{ EM.stop } }
Signal.trap('TERM'){ AMQP.stop{ EM.stop } }

but no luck.

Of course, I can just keep the sleep(2) at the end of seeds.rb, but I
would like to make sure that for any shutdown (end of script, signal
to restart the server, cron jobs, whatever) AMQP has had a chance to
push all msgs to RabbitMQ before shutting down. Any thoughts or
ideas?

thanks for your help with what I'm sure is kind of a noob question.
Adam

Aman Gupta

unread,
Nov 10, 2009, 11:27:26 PM11/10/09
to ruby...@googlegroups.com
You shouldn't run EM in a thread, or join the thread at the end of the
scripts. Alternatively, if you're just publishing messages, use a
synchronous interface like Carrot or Bunny.

Aman

Adam Greene

unread,
Nov 12, 2009, 9:40:24 PM11/12/09
to AMQP
ok. I was hesitant to use bunny just because I didn't want multiple
API's in the code base... but if that is the way to go....

Thank you for your prompt note!
Adam
Reply all
Reply to author
Forward
0 new messages