使用rabbitmq和rails,如何创建无限循环过程?

发布于 2024-10-23 01:01:11 字数 185 浏览 7 评论 0原文

在rails web应用程序中,如果我将消息写入像rabbitmq这样的队列,当生产者向队列发送消息时,如何通知客户端?

我猜我必须创建一个在后台运行的单独进程来响应正确的消息?即此代码超出了 Web 应用程序的范围。

如果是这种情况,是否可以重用 Rails 应用程序中已有的模型/库?那我必须把这段代码复制到两个地方吗?

In a rails web app, if I write messages to a queue like rabbitmq, how will clients be notified when 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-10-30 01:01:11

看起来您的应用程序需要通常所谓的后台工作进程。对于任何中等复杂的 Web 应用程序来说,这是一个相当常见的要求。

我猜我必须创建一个在后台运行的单独进程来正确响应消息?

是的 - 你是对的。虽然完全可以使用线程来处理后台任务(在您的情况下,从 RabbitMQ 读取和处理消息),但 Rails 应用程序的标准和推荐路线是运行单独的后台进程。

如果是这种情况,是否可以重用 Rails 应用程序中已有的模型/库?

绝对地。实现此功能的最简单方法是使用 Rails 的内置运行程序命令

另一种选择是创建一个 ruby​​ 脚本来加载 Rails 应用程序。例如,您可以在项目的根目录中创建文件 my_script.rb,该文件可能如下所示:

# 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 }

如果您的需求变得更加复杂,或者您发现需要运行多个后台进程以跟上您需要处理的数据量,您可能需要查看 许多可用的库和框架可以帮助解决这个问题。

创建后台进程后,当您将其部署到生产服务器时,您将需要一种连续运行它的方法。虽然可以使用像 daemons 这样的库,正如 ctcherry 所建议的,但我建议使用像 upstart(如果部署到 ubuntu)或 runit此处提供了最流行选项的详细总结

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.

我要还你自由 2024-10-30 01:01:11

你是对的,你确实需要一个后台进程。如果您愿意,您可以将该过程的代码保留在 Rails 项目的 lib 文件夹中,我之前已经这样做过,没有出现问题,并且它将相关代码保留在一起,这很好。

我使用这个库来创建我的长时间运行的进程,它非常简单:

http://daemons.rubyforge.org/

为了重用 Rails 应用程序中的模型,您可以在 config/environment.rb 文件上运行 require 来加载所有内容。 (首先将 RAILS_ENV 设置为环境变量以选择正确的环境)从那时起,脚本的行为就像您处于 Rails 控制台会话中一样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文