EDDYMENS

EDDYMENS

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 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

To prevent unauthorized access to the URL you create for the cron job, it will be important to add some form of authorization, be it a simple key you pass in as a query parameter or a proper authorization header which is also something cron-job.org supports.

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.

If you found this post useful please consider subscribing to my newsletter to know whenever I post something new 😊.