blog.dbrgn.ch

Schedule Weekly or Monthly Tasks on Heroku

written on Friday, October 04, 2013 by

Current Situation

The Heroku Cron add-on was disabled in June 2012 and existing users were migrated to the Scheduler add-on. According to Heroku, the new add-on is "superior" and should be preferred over a cronjob.

I don't know what Heroku's definition of "superior" is; the new add-on might be better integrated into the Heroku stack and more reliable, but from a user / feature perspective the Scheduler add-on is obviously much less powerful than a cronjob. The main reason for this is that jobs can only be scheduled at three pre-defined intervals: Daily, Hourly or Every 10 Minutes.

I'm currently working on a project where I absolutely need weekly and monthly intervals. And the Scheduler needs to be a dyno task, not a callback URL as supported by the Temporize Scheduler.

There's also a feature request for custom frequencies on getsatisfaction since January, but unfortunately it hasn't been replied to yet.

The Workaround

Thanks to a nice StackOverflow answer I found a workaround to this problem: Simply "protect" a daily task using a bash if-statement. Some examples:

Run a task every Monday:

if [ "$(date +%u)" = 1 ]; then MY_COMMAND; fi

Run a task every 1st day in a month:

if [ "$(date +%d)" = 01 ]; then MY_COMMAND; fi

You could also run a job every year on December 24th:

if [ "$(date +%m)" = 12 ] && [ "$(date +%d)" = 24 ]; then MY_COMMAND; fi

I think that's currently the best solution to work around this issue, much better than hardcoding weekdays in the scheduled script.

This entry was tagged heroku