Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 scheduler add-on does not allow me a weekly option.

Any suggestions on how I could accomplish this:

  • I've tried using rufus-scheduler in the past but it caused me some problems, so that's not an option. See here for detail.
  • I'm thinking of something along the lines of checking for current day within a daily job. Has anyone tried it and have feedback or know of issues with the approach?
  • Other ideas much appreciated.
like image 647
T C Avatar asked Mar 23 '12 06:03

T C


People also ask

How do I schedule a Heroku job?

Using the DashboardNavigate to the “Resources” tab of the app's Dashboard. Search for “Heroku Scheduler” in the Add-ons search box. Follow the prompts to provision the Add-on.

Is Heroku scheduler reliable?

Reliable delivery Heroku Scheduler is a free add-on, but it doesn't guarantee that jobs will be executed at their scheduled time, or at all for that matter. While it is rare, the possibility that a job may be skipped or run twice does exist.

How do I schedule a dyno in Heroku?

Click on the Heroku Scheduler Add-on from the Resources tab on the Dashboard or run the command heroku addons:open scheduler from the CLI. Click edit on your scheduled job (usually indicated with a pencil). Under Run Command, look for a dropdown that allows you to select the dyno plan. Save Job.


1 Answers

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.

like image 146
Pierre Avatar answered Sep 19 '22 21:09

Pierre