【问题标题】:What's the syntax to run a cron job on heroku every hour?每小时在 heroku 上运行 cron 作业的语法是什么?
【发布时间】:2011-08-09 14:13:27
【问题描述】:

我刚刚获得了 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

我需要知道这条带有 1 的行是否是要走的路...

if Time.now.hour % 1 == 0

提前感谢您的帮助,

最好的问候。

【问题讨论】:

    标签: ruby-on-rails cron heroku jobs


    【解决方案1】:

    如果您想每小时运行一次,请不要费心检查。 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 任务之后):

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

    这样你就可以在 cron 中运行它,如果需要也可以手动运行

    【讨论】:

      【解决方案2】:

      由于您已经有一个每小时的 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 2012-07-18
        • 2012-01-27
        相关资源
        最近更新 更多