使用 Ruby 进行异步方法调用,就像使用 Ajax 一样

发布于 2024-12-15 02:03:18 字数 882 浏览 1 评论 0原文

我正在使用 XMPP,并且有一个消息回调,在发送每条消息时都会激活该消息回调。我的目标是将消息到达的数据发送到回调中的 API,并根据响应使用 XMPP 客户端发回一些内容。

  1. 用户类型消息(浏览器聊天客户端)
  2. 消息通过 XMPP 到达服务器
  3. 消息发送到 API
  4. 收到响应
  5. 响应被发送回聊天客户端。

我的代码如下,

admin_muc_client.activate_message_callbacks do |m|            
  sender    = m.from.resource
  room_slug = m.from.node
  message   = m.body                  

  r = HTTParty.get('http://example.com/api/v1/query?msg=message')
  Rails.logger.debug(r.inspect)
  admin_muc_client.send_message("Message #{r.body}") if m.from.resource != 'admin'
end

我在这里担心的是,由于回调是事件化的,并且对 API 的请求将是阻塞调用,这可能成为整个应用程序的瓶颈。 我更喜欢使用 AJAX for Javascript 之类的东西,它会触发请求,并在响应可用时发送数据。我如何在 Ruby 中实现它?

我查看了delayed_job 和backgroundrb,它们看起来像是“即发即忘”操作的工具。我需要一些能够以异步方式与响应激活回调的东西。

我真的非常感谢有关如何实现我想要的异步行为的一些帮助。我也熟悉像 RabbitMQ 这样的消息队列,我认为这会增加显着的复杂性。

I am working with XMPP and I have a message callback which is activated on the event of every message being sent. My aim is to send the data arriving by the message to an API within the callback and based on the response send something back using the XMPP client.

  1. User type message (Browser Chat Client)
  2. Message arrives to the server via XMPP
  3. Message is sent to the API
  4. Response is received
  5. Response is sent back to the chat client.

My code for this is as follows

admin_muc_client.activate_message_callbacks do |m|            
  sender    = m.from.resource
  room_slug = m.from.node
  message   = m.body                  

  r = HTTParty.get('http://example.com/api/v1/query?msg=message')
  Rails.logger.debug(r.inspect)
  admin_muc_client.send_message("Message #{r.body}") if m.from.resource != 'admin'
end

My concern here is that since the callback is evented and the request to the API would be a blocking call this could become a bottleneck for the entire application.
I would prefer to use something like AJAX for Javascript which would fire the request and when the response is available send data. How could I implement that in Ruby?

I have looked at delayed_job and backgroundrb which look like tools for fire and forget operations. I would need something that activates a callback in an asynchronous manner with the response.

I would really really appreciate some help on how to achieve the asynchronous behavior that i want. I am also familiar with message queues like RabbitMQ which I feel would be addition of significant complexity.

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

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

发布评论

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

评论(1

当梦初醒 2024-12-22 02:03:18

你看过girl_friday吗?来自它的维基 -

girl_friday 是一个用于执行异步任务的 Ruby 库。通常,您不想通过执行某些任务(例如发送电子邮件)来阻止网络响应,因此您可以使用此 gem 在后台执行它。它适用于任何 Ruby 应用程序,包括 Rails 3 应用程序。

为什么不使用无数其他异步解决方案(Resque、dj 等)?因为 Girl_friday 比这些解决方案更简单、更高效:girl_friday 在您的 Rails 进程中运行,并使用 actor 模式来实现安全并发​​。因为它在同一个进程中运行,所以您不必监视一组单独的进程、部署单独的代码库、为这些进程浪费数百个额外的 MB RAM 等。有关更多详细信息,请参阅我对 Ruby 中的 Actor 的介绍。

您确实需要编写线程安全代码。这并不难做到:参与者模式意味着您收到一条消息并处理该消息。不存在需要锁并可能导致应用程序代码死锁的共享数据。因为 Girl_friday 确实在幕后使用了线程,所以您需要确保您的 Ruby 环境可以有效地执行线程。 JRuby、Rubinius 1.2 和 Ruby 1.9.2 对于大多数应用程序来说应该足够了。我不支持 Ruby 1.8,因为它的线程支持很差。

我想这就是您正在寻找的。

Did you look at girl_friday? From it's wiki -

girl_friday is a Ruby library for performing asynchronous tasks. Often times you don't want to block a web response by performing some task, like sending an email, so you can just use this gem to perform it in the background. It works with any Ruby application, including Rails 3 applications.

Why not use any of the zillions of other async solutions (Resque, dj, etc)? Because girl_friday is easier and more efficient than those solutions: girl_friday runs in your Rails process and uses the actor pattern for safe concurrency. Because it runs in the same process, you don't have to monitor a separate set of processes, deploy a separate codebase, waste hundreds of extra MB in RAM for those processes, etc. See my intro to Actors in Ruby for more detail.

You do need to write thread-safe code. This is not hard to do: the actor pattern means that you get a message and process that message. There is no shared data which requires locks and could lead to deadlock in your application code. Because girl_friday does use Threads under the covers, you need to ensure that your Ruby environment can execute Threads efficiently. JRuby, Rubinius 1.2 and Ruby 1.9.2 should be sufficient for most applications. I do not support Ruby 1.8 because of its poor threading support.

I think this is what you are looking for.

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