Cron Scheduling using Heroku Scheduler with Custom Intervals

Kartik Luke Singh
Jobspire Blog
Published in
3 min readMar 15, 2016

Scheduling repetitive tasks on Heroku using Heroku Scheduler while maintaining flexibility.

THEN

Around last year I had written an article on scheduling on Heroku (http://blog.jobspire.net/free-cron-scheduling/). And in it I had written the following:

We could have used Heroku Scheduler and that’s a good idea if you need to schedule tasks in intervals of ten minutes, daily or monthly.

However, for certain tasks we needed to define a much more specific execution interval. Three times a day, every two hours or even every 5 minutes.

Basically I needed to schedule tasks and Heroku Scheduler only allowed the options of Daily, Hourly and Every 10 minutes. So I created an app on Google App Engine. Google App Engine has a much simpler interface for setting up Cron tasks with a lot of control over just when you want them to run. And this scheduling app would simply make a request to my Rails application telling it to do specific tasks based on the schedule I defined.

cron:
- description: daily
url: /daily
schedule: every day 10:00

- description: bimonthly
url: /bimonthly
schedule: second,fourth mon of month 10:00

- description: monthly
url: /monthly
schedule: 1 of month 10:00

This quickly became a chore to update. I mostly built up the app to avoid the cost of other paid CRON services and to try out the Python framework Flask.

NOW

So we’ve updated our process to use Heroku Scheduler. Heroku scheduler spins up a one-off dyno that runs a command, in this case I’m running a rake task. You can learn how to add it to your project and use it here. You should end up with something like this:

Heroku Scheduler Dashboard

The following code is in Ruby. Rake is a tool you can use with Ruby projects. It allows you to use ruby code to define “tasks” that can be run in the command line. We now handle daily, bimonthly and monthly as such, we trigger the task everyday but on days that match the parameters we want the tasks are run:

task daily: [:environment] do
# Daily tasks
end
task bimonthly: [:environment] do
# If second week or fourth week of the month
if Time.now.day == 14 || Time.now.day == 28
# Weekly tasks
end
end
task monthly: [:environment] do
# If first of the month
if Time.now.day == 1
# Monthly tasks
end
end

Hence I was able to get the flexibility I wanted out of Heroku Scheduler. The caveats are that you can’t run anything more often than every ten minutes and Heroku warns that if you scheduling is critical to your app you should run a custom clock process.

NOTE ON COST:

Heroku allows you a certain number of free dyno-hours. If you’re scheduling process ends up running less than that, scheduling is essentially free. Otherwise costs will be related to use. A task running once every day is unlikely to get you charged, but one running every ten minutes might.

If you’re looking to hire people for your startup and don’t have the time to source and filter through thousands of applicants. Check out Jobspire!

Say hi on Twitter. If you liked this article, please hit recommend so it can reach others.

--

--

Kartik Luke Singh
Jobspire Blog

Full Stack Developer, UI/UX Enthusiast. Mostly harmless. Currently working at @reflektive. Made in India, 1993.