与 Ruby Web 套接字服务器串联排队
我正在 Rails 上使用 jruby 编写一个应用程序。应用程序的一部分从网页启动长时间运行的进程。在某些情况下,长时间运行的过程可能会持续 20 分钟,但在大多数情况下,会比网页响应的时间更长。如果用户关闭浏览器,我还希望工作继续进行。长时间运行的进程将在运行时将记录添加到数据库中。
我想在网页上给出数据库插入的可视化指示,并且我更喜欢使用 Web 套接字而不是轮询数据库中的插入。
我正在考虑使用队列处理程序向 resque 队列发送一条消息,该队列处理程序将确保在用户关闭浏览器时完成作业。队列处理程序将执行到数据库的插入。
我正在考虑使用 EM-WebSocket 作为我的 websocket 服务器。
我遇到的问题是:
如何在 resque 进程和 EM-WebSocket 进程之间进行通信?我想以某种方式将新插入到数据库的详细信息从 resque 进程传递到将与浏览器通信的 EM-WebSocket 实例?
有人解决了这样的问题或者有什么想法我可以如何做到这一点?
I am writing an application using jruby on rails. Part of the application initiates a long running process from a web page. The long running process could last for 20 minutes in some cases but will in most cases outlive a web page response in terms of time. I also want the job to continue if the user closes down the browser. The long running process will add records to a database as it is running.
I want to give visual indications of the inserts into the database on the web page and I would prefer to use web sockets rather than polling the database for the inserts.
I am thinking of sending a message to a resque queue with a queue handler that will ensure the job is completed if the user closes down the browser. The queue handler will perform the inserts into the database.
I was thinking of using EM-WebSocket as my websocket server.
The problem I have is:
How can I communicate between the resque process and EM-WebSocket process? I want to somehow pass the details of the new inserts into the database from the resque process to an EM-WebSocket instance that will communicate with the browser?
Anybody solved a problem like this or any ideas how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上正在开发一个 gem,它使这变得非常简单。现在它还很简陋,但它确实有效。 https://github.com/KellyMahan/RealTimeRails
它运行一个事件机服务器来侦听更新和使用 em-websockets 将这些更新发送到浏览器。
它的目的是通过 after_save 调用来监视活动记录更新,该调用告诉事件机服务器它的模型有一个带有 id 的更新。然后,它将模型和 ID 与特定通道进行匹配,以将消息发送到 Web 套接字服务器上的连接。当浏览器收到更新通知时,它会调用 ajax 来检索最新结果。
仍在进行中,但它可以帮助您。
I'm actually working on a gem that makes this pretty simple. Right now it's pretty bare, but it does work. https://github.com/KellyMahan/RealTimeRails
It runs an event-machine server for listening for updates and makes use of em-websockets to send those updates to the browser.
It's meant to watch for active record updates through an after_save call that tells the event-machine server it has an update for it's model with an id. Then it matches the model and id to specific channels to send a message to connections on the web socket server. When the browser receives a notice to update it makes an ajax call to retrieve the latest results.
Still a work in progress but it could help you.