我目前正在创建一个rails应用程序,并希望了解如何安排在特定时间运行某些任务.
对于例如管理员希望在早上8:00向他的用户发送电子邮件,并且不想提前醒来发送该电子邮件.因此,他/她希望安排任务在那时发送电子邮件.
那么,有没有一种方法,或者更好的仍然是宝石,允许任务被安排在特定时间运行?谢谢!
解决方法
我首选的解决方案是
Whenever宝石.
有了它,你有schedule.rb文件,它指定何时应该完成某些事情,包括运行rake任务,执行方法,甚至执行任意shell命令:
示例schedule.rb文件,从Whenever自述文件中无耻地复制:
every 3.hours do runner "MyModel.some_process" rake "my:rake:task" command "/usr/bin/my_great_command" end every 1.day,:at => '4:30 am' do runner "MyModel.task_to_run_at_four_thirty_in_the_morning" end every :hour do # Many shortcuts available: :hour,:day,:month,:year,:reboot runner "SomeModel.ladeeda" end every :sunday,:at => '12pm' do # Use any day of the week or :weekend,:weekday runner "Task.do_something_great" end every '0 0 27-31 * *' do command "echo 'you can use raw cron Syntax too'" end # run this task only on servers with the :app role in Capistrano # see Capistrano roles section below every :day,:at => '12:20am',:roles => [:app] do rake "app_server:task" end