2010-08-11 75 views

回答

5

看一看的宝石时,http://github.com/javan/whenever

这是伟大的与轨道具有明显的DSL自动后台任务。我们几个月来一直在使用它的产品,它的工作原理非常轻巧。从他们的README一些例子:

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 

自述是很透彻,但也railscasts一个很好的截屏:http://railscasts.com/episodes/164-cron-in-ruby

它很容易与下面的代码Capistrano的集成(从README复制):

after "deploy:symlink", "deploy:update_crontab" 

    namespace :deploy do 
    desc "Update the crontab file" 
    task :update_crontab, :roles => :db do 
     run "cd #{release_path} && whenever --update-crontab #{application}" 
    end 
    end 

就机器而言,您可以使用本地配置文件,甚至可以在部署时使用符号链接config/schedule.rb文件。我想我会包括将在部署local_schedule.rb被符号链接,然后把这个在的config/schedule.rb

if File.exists?(File.dirname(__FILE__) + '/config/local_schedule.rb') 
    require File.dirname(__FILE__) + '/local_schedule.rb' 
end 
顶部的本地文件

你的日程安排将运行,但随后包括任何地方,只要确保它之前的符号链接上面的帽子任务运行,你应该很好去。

我希望这有助于!

+0

我爱你的答案和railscast,谢谢!我唯一的一个诀窍是,做机器特定的东西的方式有点太复杂。由于Schedule.rb允许我们编写ruby代码,因此我想知道是否有方法来指定它。 我在这里问了一个相关的问题:http://stackoverflow.com/questions/3550770/finding-out-deployment-machine-you-are-on-in-code-rails 我有道理吗? – 2010-08-23 19:07:19