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.
As discussed over here, and using the above logic from Rob, here are the bash scripts broken down by a day of the week, once a month, and on a specific date.
Run a task every Monday:
Run a task every 1st day in a month:
You could also run a job every year on December 24th:
It's not ideal, but I've taken to adding a RUN_IF environment variable to rake tasks run through heroku:scheduler which lets me weekly and monthly schedules for jobs.
If a rake task is run without a RUN_IF variable it will run. Otherwise, the job will be aborted unless the value of RUN_IF evals to a falsey value.
Similar to other ideas above, but I prefer moving the scheduling details out of the application code.
If you don't want or cannot do this with Rake, another option is to do this with Ruby from bash:
This works even for non-ruby projects (as in my case: Clojure).
One approach would be the one of your 2nd bullet point:
activate the Heroku cron add-on, and add acron.rake
task inapp/lib/tasks
Activate the Heroku scheduler add-on, and add a
scheduler.rake
task inapp/lib/tasks
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.
An alternate option using only shell code. Setup the Heroku scheduler hourly, and do a comparison against the date command:
I've used this code to mimic cron in my local time zone: