每小时在heroku上运行一个cron作业的语法是什么?

时间:2023-01-02 01:09:05

I just acquired the paid version of Heroku's cron, in order to run some tasks every hour. What I would like to know is the syntax I should use to actually get it to run every hour. So far I have this, Could you please tell me if it's correct:

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

I need to know if this line with the 1 in it is the way to go...

我需要知道这行中的1是否是要走的路......

if Time.now.hour % 1 == 0

Thanks in advance for your help,

在此先感谢您的帮助,

Best Regards.

2 个解决方案

#1


2  

If you want to run every hour, don't bother checking. Heroku will run it hourly.

如果你想每小时跑一次,不要费心去检查。 Heroku将每小时运行一次。

Since %1 will always return 0, better to just have:

由于%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

Also, if you want to be able to run Video.update_view_counts when you need to, you could instead (after creating the rake task):

此外,如果您希望能够在需要时运行Video.update_view_counts,则可以改为(在创建rake任务之后):

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

That way you can run it inside of cron, and manually if needed

这样你可以在cron中运行它,并在需要时手动运行

#2


1  

Since, you already have an hourly cron, you don't have to check for the time to run the code.

因为,你已经有一个每小时的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

#1


2  

If you want to run every hour, don't bother checking. Heroku will run it hourly.

如果你想每小时跑一次,不要费心去检查。 Heroku将每小时运行一次。

Since %1 will always return 0, better to just have:

由于%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

Also, if you want to be able to run Video.update_view_counts when you need to, you could instead (after creating the rake task):

此外,如果您希望能够在需要时运行Video.update_view_counts,则可以改为(在创建rake任务之后):

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

That way you can run it inside of cron, and manually if needed

这样你可以在cron中运行它,并在需要时手动运行

#2


1  

Since, you already have an hourly cron, you don't have to check for the time to run the code.

因为,你已经有一个每小时的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