[英]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
。
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.