Automating Processes Through Heroku Scheduler

Jacob Kenny
4 min readAug 23, 2020
Photo by Moritz Kindler on Unsplash

For an application that I was recently working on, I found myself tied to my computer, having to manually log in and run a command every three hours, which would allow me to obtain and then save exchange rate information into a database. Sometimes I would forget to do this task, or simply be away from my computer, and so my data became uneven. Rather than having uniform data with clear intervals between each piece, I had inconsistently obtained data which although could still be used, was not beautiful, nor would it be appropriate for usage in a more formal setting. So what do we do? — do I have to redouble my efforts to stay beside my computer so that I can run this command at the right times? Nah, son. We gotta automate.

Before deploying my application, which utilizes a Rails backend to store data, I was able to implement the Whenever gem to set up some a cron job to run the command. This is an excellent way to manage the task when everything is contained to my local machine, but thinking of the bigger picture here, I need for this application to work for someone who does not have access to my personal 2017 Macbook air. The solution that I ended up going with is Heroku Scheduler. The linked article very helpful and I suggest reading it in full, but for the quick and dirty version simply keep reading. Inside of the lib/tasks folder, create a file called scheduler.rake. In this new file, we will be defining our rake task(s), which will then be executed by the Heroku scheduler. My scheduler.rake file looks like this:

On line two, I name the task “fetch_rates,” and then on line four, I have a model “Rate” for which I call a class method “getRates.” Lines 1, 3, and 5 are really not needed, but I like to pretend that my terminal is talking back to me when I run a command, so I have the print statements. The ‘desc’ (short for description) will be what we see if we run the terminal command “rake -T” to see a list of all the available rake comands. My ‘Rate’ model referenced in the rake command is written as follows:

At this point I could run the command “rake fetch_rates” in the terminal, and should see that the task is executed. I suggest doing this before moving on just to make sure that everything is working as intended. The final step is to link it all up with the Heroku Scheduler add-on so that the task will be run automatically at set intervals.

Although it is possible to set up the scheduler through the Heroku CLI, I’ve found that I prefer to do it directly through the website — for me it makes the whole thing easier to visualize, and eliminates some uncertainty as I can immediately see the effects of my actions. Go to the application’s dashboard on Heroku, and under the resources tab there is a search bar, where we can locate the scheduler add-on. Agree to the terms of service, and then you will be taken to a screen (pictured below) where all we have to do is set the time interval to run the command, and the command that needs to be executed. In my case that command was “rake fetch_rates,” and I wanted it to be done every hour. The scheduler allows for the interval to be set for every 10 minutes, every hour at a specific minute, or every day at a specific hour. This is a little bit confusing, but for example if I had to complete the task every 3 hours, I could set up eight jobs individually, and run every day at the hours 00, 03, 06, 09, 12, 15, 18, and 21 respectively. Heroku is also clear in that even if you add a task to run every day at 10:00am, there may be slight variances in the exact time of completion. Personally, I’ve never noticed a difference of more than a few minutes off, so it hasn’t been a problem for me, but this certainly could be something to look out for if we’re looking for a higher level of precision…

Heroku Scheduler

I hope that this has been helpful, and as always, let me know if I’ve missed anything!

--

--