Rails App 在后台运行长时间任务的最佳实践? [英] Best practice for Rails App to run a long task in the background?

查看:26
本文介绍了Rails App 在后台运行长时间任务的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 应用程序,不幸的是,在向控制器发出请求后,必须进行一些需要一段时间的处理.Rails 中为长期运行的任务或请求提供反馈或进度的最佳实践是什么?这些控制器方法通常持续 60 秒以上.

I have a Rails application that unfortunately after a request to a controller, has to do some crunching that takes awhile. What are the best practices in Rails for providing feedback or progress on a long running task or request? These controller methods usually last 60+ seconds.

我不关心客户端...我计划每隔一秒左右就有一个 Ajax 请求并显示一个进度指示器.我只是不确定 Rails 的最佳实践,我要创建一个额外的控制器吗?有什么聪明的我可以做吗?我希望答案只集中在使用 Rails 的服务器端.

I'm not concerned with the client side... I was planning on having an Ajax request every second or so and displaying a progress indicator. I'm just not sure on the Rails best practice, do I create an additional controller? Is there something clever I can do? I want answers to focus on the server side using Rails only.

预先感谢您的帮助.

如果重要,http 请求是针对 PDF 的.然后我让 Rails 与 Ruport 一起生成这些 PDF.问题是,这些 PDF 非常大并且包含大量数据.使用后台任务还有意义吗?让我们假设平均 PDF 需要大约一分钟到两分钟,这是否会使我的 Rails 应用程序在这段时间内对任何其他服务器请求无响应?

If it matters, the http request are for PDFs. I then have Rails in conjunction with Ruport generate these PDFs. The problem is, these PDFs are very large and contain a lot of data. Does it still make sense to use a background task? Let's assume an average PDF takes about one minute to two minutes, will this make my Rails application unresponsive to any other server request during this time?

编辑 2:

好的,经过进一步调查,似乎我的 Rails 应用程序在收到大型 PDF 的请求后确实对任何其他 HTTP 请求没有响应.所以,我想现在的问题变成了:最好的线程/后台机制是什么?它必须是稳定和维护的.我很惊讶 Rails 没有内置这样的东西.

Ok, after further investigation, it seems my Rails application is indeed unresponsive to any other HTTP requests after a request comes in for a large PDF. So, I guess the question now becomes: What is the best threading/background mechanism to use? It must be stable and maintained. I'm very surprised Rails doesn't have something like this built in.

编辑 3:

我已阅读此页面:http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails.我很想了解使用这些工具的各种体验.

I have read this page: http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails. I would love to read about various experiences with these tools.

编辑 4:

如果重要的话,我正在使用Passenger Phusionmodrails".

I'm using Passenger Phusion "modrails", if it matters.

编辑 5:

我的开发机器使用的是 Windows Vista 64 位;但是,我的生产机器是 Ubuntu 8.04 LTS.我应该考虑为我的开发机器切换到 Linux 吗?所提供的解决方案是否适用于两者?

I'm using Windows Vista 64 bit for my development machine; however, my production machine is Ubuntu 8.04 LTS. Should I consider switching to Linux for my development machine? Will the solutions presented work on both?

推荐答案

Workling 插件允许您在队列中安排后台任务(它们将执行冗长的任务).从 0.3 版本开始,您可以询问工作人员的状态,这将允许您显示一些漂亮的进度条.

The Workling plugin allow you to schedule background tasks in a queue (they would perform the lengthy task). As of version 0.3 you can ask a worker for its status, this would allow you to display some nifty progress bars.

Workling 的另一个很酷的功能是可以切换异步后端:您可以使用 DelayedJobs、Spawn(经典分支)、Starling...

Another cool feature with Workling is that the asynchronous backend can be switched: you can used DelayedJobs, Spawn (classic fork), Starling...

这篇关于Rails App 在后台运行长时间任务的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆