简体   繁体   English

我有一个运行40分钟的Ruby脚本,如何将其集成到Rails应用程序中?

[英]I have a Ruby script that takes 40 mins to run, how do I integrate this into a Rails App?

I have a Ruby script that crawls a site, and takes 40 minutes. 我有一个抓取网站的Ruby脚本,需要40分钟。 What I would like to do is dump the results into a database and be able to do regular ActiveRecord stuff on the data. 我想做的是将结果转储到数据库中,并能够对数据进行常规的ActiveRecord

  1. If I put it in a Rails app and have a 'start' button that initiates the script, will it time out? 如果我把它放在一个Rails应用程序中,并有一个启动脚本的“开始”按钮,它会超时吗?
  2. Ideally I want the script to run at least once a day - and update the db - so would my best bet be to create a rake task, or is there some other way to do this? 理想情况下,我希望脚本每天至少运行一次 - 并更新数据库 - 所以我最好的选择是创建一个rake任务,还是有其他方法可以做到这一点?
  3. If I am wrapping this in a Rails App, in which folder should I put the script and what is the best way to approach it? 如果我将它包装在Rails应用程序中,我应该在哪个文件夹中放置脚本以及处理它的最佳方法是什么? I can't drop it in a model file - because that doesn't make any sense. 我不能把它放在模型文件中 - 因为这没有任何意义。
  4. I have never done any 'Job' type processing before, but this sounds like it may fall in that purview. 我之前从未做过任何“工作”类型的处理,但这听起来可能属于该范围。 What other things should I consider when doing this? 这样做时我还应该考虑其他什么?

Edit 1: 编辑1:

Another question is, if I put this particular Ruby script in my /lib directory, how do I get it to interact with the DB? 另一个问题是,如果我将这个特定的Ruby脚本放在我的/lib目录中,我该如何让它与数据库进行交互? I usually interact with the DB from the model and controller. 我通常从模型和控制器与DB交互。 How would I store the results in my DB after it is run? 如何在运行后将结果存储在我的数据库中?

I'll try to give you some straightforward answers. 我会试着给你一些直截了当的答案。

A) You would most likely put it as a background job. A)你很可能将它作为后台工作。 There are some decent gems for that. 有一些体面的宝石。 Consider https://github.com/defunkt/resque or something more lightweight such as https://github.com/tobi/delayed_job . 请考虑https://github.com/defunkt/resque或更轻量级的内容,例如https://github.com/tobi/delayed_job

B) A rake task would suffice and then run it with either of the mentioned libraries. B)rake任务就足够了,然后用任何一个提到的库运行它。 Another option would be a cron job. 另一种选择是cron工作。

C) You should put it in the lib/ directory. C)你应该把它放在lib /目录中。

D) You should always have some kind of processing log available in order to track potential errors. D)您应始终提供某种处理日志,以便跟踪潜在的错误。 Be sure to read the instructions properly if you choose any of the two libraries mentioned above. 如果您选择上述两个库中的任何一个,请务必正确阅读说明。

Easiest is to set up the job as a rake task. 最简单的方法是将作业设置为rake任务。 Then test the rake task from the command line to ensure that it works. 然后从命令行测试rake任务以确保它工作。

Finally, add the rake task as a cronjob on your server and have yourself emailed the resulting log. 最后,将rake任务添加为服务器上的cronjob,并通过电子邮件发送生成的日志。

For my app, I have a "Nightly" rake task that handles several sub-tasks. 对于我的应用程序,我有一个“夜间”rake任务,可以处理几个子任务。 It does various clean-up tasks, runs the nightly reports etc. 它执行各种清理任务,运行夜间报告等。

A job processing system such as Delay Job would probably be overkill for a once-a-day type task. 对于每天一次的类型任务,诸如延迟作业之类的作业处理系统可能会过度杀伤。 My app also uses delay job, but for many many client initiated tasks throughout the day. 我的应用程序也使用延迟作业,但对于许多客户端启动的任务全天。

Remember that you also need a process to watch the delay job process (and re-start it if necessary). 请记住,您还需要一个进程来观察延迟作业过程(并在必要时重新启动它)。 I use runit , there are other alternatives too. 我使用runit ,还有其他选择。

每当宝石做cron工作时使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行ruby脚本,我将其放入我的Rails应用程序中的/ lib / tasks /目录中一次? - How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once? 如何将红宝石程序调用到Rails应用程序中? - How do I have a ruby program call into a rails app? 当我已经在相同的ruby版本中安装了rails 3和4时,如何创建新的rails 3应用程序 - How do i create new rails 3 app when i have already have rails 3 and 4 installed in same ruby version 如何将InDesign PDF集成到我的rails应用程序中 - How do I integrate an InDesign PDF into my rails app 如何下载并运行Rails应用程序? - How do I download and run a rails app? 尝试在Rails开发中建立一个简单的Nginx红宝石。 如何运行该应用程序? - Trying to set up a simple nginx ruby on rails development. How do I run the app? 如何从我的Rails 3应用程序的/ lib /文件夹中的ruby脚本中访问我的某个模型? - How do I access one of my models from within a ruby script in the /lib/ folder in my Rails 3 app? 如何将独立的Rack应用程序安装到Ruby On Rails应用程序中? - How do I mount a standalone Rack app into a Ruby On Rails app? 我已经在ruby中集成了网络资源支付网关,但我希望与rails集成 - I have integrate cybersource payment gateway in ruby but i want integrate with rails 我如何在Ruby on Rails中使用插件架构? - How do I have plugin architecture in Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM