在Rails环境中运行计划任务的最佳方法是什么?脚本/跑步吗?耙?我想每隔几分钟运行一次任务。
当前回答
你可以使用resque和resque- scheduling gem来创建cron,这很容易做到。
https://github.com/resque/resque
https://github.com/resque/resque-scheduler
其他回答
你可以使用resque和resque- scheduling gem来创建cron,这很容易做到。
https://github.com/resque/resque
https://github.com/resque/resque-scheduler
Script /runner和rake任务完全可以作为cron作业运行。
在运行cron作业时,您必须记住一件非常重要的事情。它们可能不会从应用程序的根目录被调用。这意味着你对文件(而不是库)的所有需求都应该通过显式路径完成:例如File.dirname(__FILE__) + "/other_file"。这也意味着你必须知道如何显式地从另一个目录调用它们:-)
检查您的代码是否支持从另一个目录运行
# from ~
/path/to/ruby /path/to/app/script/runner -e development "MyClass.class_method"
/path/to/ruby /path/to/rake -f /path/to/app/Rakefile rake:task RAILS_ENV=development
此外,cron作业可能不会像你那样运行,所以不要依赖于你在.bashrc中放入的任何快捷方式。但这只是一个标准的cron提示;-)
使用Craken(以rake为中心的cron作业)
我使用脚本运行cron,这是运行cron的最好方式。 这里有一些cron的例子,
打开CronTab - > sudo CronTab -e
并粘贴波纹线:
00 00 * * * wget https://your_host/some_API_end_point
这里有一些cron格式,对你有帮助
::CRON FORMAT::
Examples Of crontab Entries
15 6 2 1 * /home/melissa/backup.sh
Run the shell script /home/melissa/backup.sh on January 2 at 6:15 A.M.
15 06 02 Jan * /home/melissa/backup.sh
Same as the above entry. Zeroes can be added at the beginning of a number for legibility, without changing their value.
0 9-18 * * * /home/carl/hourly-archive.sh
Run /home/carl/hourly-archive.sh every hour, on the hour, from 9 A.M. through 6 P.M., every day.
0 9,18 * * Mon /home/wendy/script.sh
Run /home/wendy/script.sh every Monday, at 9 A.M. and 6 P.M.
30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup
Run /usr/local/bin/backup at 10:30 P.M., every weekday.
希望这对你有帮助:)
最好的方法可能是使用rake来编写所需的任务,然后通过命令行执行它。
你可以在railscasts看到一个非常有用的视频
也可以看看其他资源:
Rails Rake教程
推荐文章
- 如何确定一个数组是否包含另一个数组的所有元素
- Rails find_or_create_by多个属性?
- Rails ActiveRecord日期之间
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 对于PostgreSQL表来说,多大才算太大?
- Rails:如何在Rails 4中引用CSS中的图像
- 如何使用RSpec的should_raise与任何类型的异常?
- Rails -嵌套包括活动记录?
- Delete_all vs destroy_all
- Rails获取“each”循环的索引
- 如何通过脚本创建crontab
- 在rails redirect_to中传递参数
- Rails new vs create
- Rails has_many的别名