开发者

using rabbitmq with rails, how to create the endless loop process?

In a rails web app, if I write messages to a queue like rabbitmq, how will clients be notified wh开发者_开发知识库en a producer sends a message to the queue?

I'm guessing I have to create a seperate process that runs in the background to respond to messages correct? i.e. this code is outside of the scope of a web application.

If this is the case, is it possible to re-use the models/libs that are in the rails application already? do I have to copy this code in 2 places then?


It looks like your application requires what's usually called a background or worker process. This is a fairly common requirement for any moderately complex web application.

I'm guessing I have to create a seperate process that runs in the background to respond to messages correct?

Yes - you're right about this. Whilst it's perfectly possible to use threads to handle the background tasks (in your case, reading and processing messages from RabbitMQ), the standard and recommended route for a Rails application is to run a separate background process.

If this is the case, is it possible to re-use the models/libs that are in the rails application already?

Absolutely. The simplest possible way to get this working is by using Rails' built in runner command.

Another option is to create a ruby script which loads up your Rails application. For example, you could create the file my_script.rb in the root of your project, which might look something like this:

# Load my application:
require File.join(File.dirname(__FILE__), 'config/environment.rb')

# Now you can access your Rails environment as normal:
MyModel.all.each { |x| x.do_something }

If your needs become more complex, or you find that you need to run more than one background process to keep up with the volume of data you need to process, you might want to look at one of the many available libraries and frameworks which can help with this.

Once you've created your background process, you'll need a way to run it continuously when you deploy it to your production server. Whilst it's possible to use libraries like daemons, as suggested by ctcherry, I would recommend using a dedicated tool like upstart (if deploying to ubuntu) or runit. A good summary of the most popular options is available here.


You are correct, you do need a background process. And you can keep the code for that process in the lib folder of the Rails project if you like, I have done that before without issue, and it keeps related code together which is nice.

I used this library to create my long running process, it was quite simple:

http://daemons.rubyforge.org/

In order to re-use models from your rails application you can run a require on the config/environment.rb file to get everything loaded. (Set RAILS_ENV as an environment variable first to select the correct envrionement) From that point the script behaves as though you are inside a rails console session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜