2011-08-09 29 views
0

我剛剛購買了Heroku的cron的付費版本,以便每小時運行一些任務。我想知道的是我應該使用的語法實際上讓它每小時運行一次。到目前爲止,我有這個,你能告訴我,如果這是正確的:每小時在heroku上運行cron作業的語法是什麼?

desc "Tasks called by the Heroku cron add-on" 
task :cron => :environment do 
    if Time.now.hour % 1 == 0 # run every sixty minutes? 

    puts "Updating view counts...." 
    Video.update_view_counts 
    puts "Finished....." 

    puts "Updating video scores...." 
    VideoPost.update_score_caches 
    puts "Finished....." 

    puts "Erasing videos....." 
    Video.erase_videos! 
    puts "Finished....." 

    end 

    if Time.now.hour == 0 # run at midnight 

    end 
end 

我需要知道,如果在它的這條線是要走的路...

if Time.now.hour % 1 == 0 

在此先感謝您的幫助,

最好的問候。

回答

2

如果您想每小時運行一次,請不要打擾檢查。 Heroku會每小時運行一次。

自%1將始終返回0,最好只具有:

desc "Tasks called by the Heroku cron add-on" 
task :cron => :environment do 
    puts "Updating view counts...." 
    Video.update_view_counts 
    puts "Finished....." 
    #... 

    if Time.now.hour == 1 #1am 
    #... 
    end 

end 

另外,如果你希望能夠當你需要運行Video.update_view_counts,你可以代替(創建後耙任務):

Rake::Task["video:update_view_counts"].invoke 

這樣,你可以cron的內部運行它,如有必要,手動

1

,因爲你已經有一個小時的cron,你不必檢查運行時間代碼。

task :cron => :environment do 

    #<--- hourly cron begins 
    puts "Updating view counts...." 
    Video.update_view_counts 
    puts "Finished....." 

    puts "Updating video scores...." 
    VideoPost.update_score_caches 
    puts "Finished....." 

    puts "Erasing videos....." 
    Video.erase_videos! 
    puts "Finished....." 
    #hourly cron ends ---> 

    if Time.now.hour == 0 # run at midnight 

    end 

end