Last updated 2022-05-11 19:24:09

Heroku Cron Jobs Made Easy And Free

Heroku recommends using the scheduler [↗] add-on to create and run cronjobs should you need one.

But also points out the untimeliness [↗] of this addon, which raises a red flag as the whole point of setting up a cron job is to run the given task around a set time interval.

heroku-cron-job-scheduler [→]

An alternative solution

Cron-Jobs.org [↗] provides a free hosted cron-job service and the way it works is you provide a URL to be pinged at your given time interval.

heroku-cron-job-scheduler [→]

This means all you have to do is create a route/URL in your app that when pinged will execute the code responsible for completing the scheduled task.

So say you will like to check for expired subscriptions, you will put this code in say the controller of your app and assigned a dedicated route that when pinged will cause the subscription check code to execute. You will then provide this URL to Cron-Jobs.org [↗] and set the date and time that URL should be pinged and that's it!

How reliable is it?

I have personally been using this service for over 5 years now and it has yet to fail me. Also, I like the fact that the code for this is opened sourced [↗] and you can always spin up your own version should you ever need to.

A few important things to note

cron-job. org also supports proper authorization headers that can be passed in as a query parameter to prevent unauthorized access to the URL you create for the cron job.

Another key thing to remember is instead of just executing your task whenever the URL is pinged it's best to keep track of the last time or next time the tasks need to be run.

For example, say you set up the cron job to send out newsletter emails, you don't want it to be executed every time the URL is hit rather when the URL is hit you should first check to see if the time is right to send out the emails.

Here is another article you might like 😊 "How to allow the use of tabs in a textarea "