简体   繁体   English

如何使用Rails设置Cron作业?

[英]how do i set up cron jobs with rails?

I have to implement a fairly database intensive task periodically. 我必须定期执行一个相当耗费数据库的任务。 I am using rails for the application to which this task is related and thus for the sake of easiness and uniqueness in approach I want to know how can I best implement cron job with rails. 我正在将Rails用于与此任务相关的应用程序,因此,为了使方法简单易行,我希望知道如何最好地使用Rails实施cron作业。 I am aware that rake is one solution, but am totally unaware of how to use it. 我知道rake是一种解决方案,但是完全不知道如何使用它。

Thanks. 谢谢。

javan-whenever is another good thing to look at. javan-whenever是另一个值得一看的东西。

http://github.com/javan/whenever http://github.com/javan/whenever

Just implement your script to do whatever it is you need, and then invoke it with script/runner from a cron job. 只需实施您的脚本以执行所需的任何操作,然后使用cron作业中的script/runner调用它即可。

0 * * * * cd /path/to_your/app && RAILS_ENV=production script/runner scripts/my_maintenance_script.rb

That'll kick off my_maintenance_script.rb in the context of your production environment once per hour. 这将在您的生产环境的上下文中每小时启动一次my_maintenance_script.rb Pretty bare-metal, but it'll get the job done. 相当裸机,但可以完成工作。

rubber , a Capistrano plugin for cloud deployment, provides a set of helper scripts for managing cron jobs. rubber (用于云部署的Capistrano插件)提供了一组用于管理cron作业的帮助程序脚本。 These scripts will work completely independently of rubber, however. 但是,这些脚本将完全独立于橡胶工作。

If you grab the "cron-rake", "cron-runner", and "cron-sh" scripts from http://github.com/wr0ngway/rubber/tree/master/lib/generators/vulcanize/templates/base/script/ and place them in the script/ directory of your Rails project, you should be good to go. 如果您从http://github.com/wr0ngway/rubber/tree/master/lib/generators/vulcanize/templates/base/中获取“ cron-rake”,“ cron-runner”和“ cron-sh”脚本script /并将它们放置在Rails项目的script /目录中,应该很好。 Don't forget to make them executable. 不要忘记使它们可执行。

Most of the real work is done in "cron-sh." 大多数实际工作都是在“ cron-sh”中完成的。 "cron-rake" is a convenience wrapper for running rake tasks. “ cron-rake”是用于运行rake任务的便捷包装器。 "cron-runner" is a convenience wrapper for running arbitrary Ruby (much like script/runner provides). “ cron-runner”是用于运行任意Ruby的便捷包装器(非常类似于脚本/运行器提供的)。

delay_job很好,它将作业的详细信息存储在数据库中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM