关于使用resque的一些问题
我正在使用 Resque 运行后台进程。这就是我的后台进程的工作方式:
- 扫描 ActiveRecord 模型中的所有行
- 检查条件
- 如果满足条件则更新行
这需要无限地进行下去。
这就是我尝试使用 Resque 来达到我的目的的方式,这是我的工作类:
class ThumbnailMaker
@queue = :thumbnail_queue
def self.perform()
MyObj.check_thumbnails(root_url)
end
end
我了解 perform() 方法将任务保留在队列中,该任务会定期运行。就我而言,我需要一个扫描整个表的任务,因此它运行的时间更长。它能很好地满足我的要求吗?
另一方面,我需要 Rails 应用程序的根 url,可以使用 Rails 控制器中的 root_url
轻松获取该根 url。但我在我创建的课程中需要它,你能建议我如何在这里得到它吗?
I am using Resque to run a background process. This is how my background process works:
- Scans through all the rows in an ActiveRecord model
- Checks for a condition
- Updates the row if the condition is met
And this needs to go on infinitely.
This is how I am trying to use Resque for my purpose, here's my worker class:
class ThumbnailMaker
@queue = :thumbnail_queue
def self.perform()
MyObj.check_thumbnails(root_url)
end
end
I understand the perform()
method keeps a task in a queue, which is run periodically. In my case, I need a task that scans the whole table, so it runs for a longer time. Is it a good solution to my requirements?
On another note, I need the root url for my Rails application, which is easily obtained with the root_url
in Rails Controller. But I need it in a class I have created, can you suggest me how I can get it here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Resque 用于对要在后台运行的任务进行排队;队列中的每个项目运行一次,然后被删除。您想要的更像是计划任务 - 例如,自定义 Rake 任务 或其他不时运行的脚本;有许多调度宝石可用于此类事情(wenever 非常流行)或者只使用 cron。关于这个主题,有一个很棒的 RailsCasts 剧集。
Resque is for queueing tasks to be run in the background; each item in the queue runs once and then is removed. What you want is more like a scheduled task--for example, a custom Rake task or other script that runs from time to time; there are many scheduling gems available for this kind of thing (wenever is very popular) or just use cron. There is a great RailsCasts episode about this very topic.
您可能想尝试将代码放入 rake 任务中并通过 cron 作业定期运行它。 Resque/Redis 似乎有点无法满足您的需求。
如果您通过控制器调用您的类,您可以考虑将根 url 作为参数传递。否则,您可能希望将其设置为 ENV 设置并相应地配置每个部署。
You might want to try putting your code in a rake task and running it periodically through a cron job. Resque/Redis seems a bit too much for your needs.
You may consider passing the root url in with as parameter if you are calling your class through your controller. Otherwise, you may want to set it as a ENV setting and configure each of your deployments accordingly.