How can I schedule a 'weekly' job on Heroku?

前端 未结 5 1797
走了就别回头了
走了就别回头了 2020-12-24 08:49

I have a Rails app deployed on Heroku with the Heroku scheduler add-on successfully working for daily jobs.

Now I want a weekly job, but the schedul

5条回答
  •  盖世英雄少女心
    2020-12-24 09:16

    One approach would be the one of your 2nd bullet point:

    activate the Heroku cron add-on, and add a cron.rake task in app/lib/tasks

    Activate the Heroku scheduler add-on, and add a scheduler.rake task in app/lib/tasks

    task :your_weekly_task=> :environment do
      if Time.now.friday? # previous answer: Date.today.wday == 5
        #do something
      end
    end
    

    You even have the luxury of defining the day you want your task to run ;o) (5 is for Friday)

    EDIT: by the way, Cron is deprecated and Heroku recommends switching to their Scheduler add-on. This doesn't change the approach for a weekly job though.

    EDIT2: adjustments to my answer based on feedback from sunkencity.

提交回复
热议问题