Scheduling Tasks on Heroku

Sriesh Agrawal
JavaScript in Plain English
3 min readMay 15, 2021

--

Running Cron tasks on Heroku for free using Heroku scheduler

Steps Involved

  1. Running Cron tasks locally using node-cron
  2. Checking the task to be executed locally
  3. Deploying the changes on the Heroku server
  4. Scheduling the task on Heroku via Heroku Scheduler

Cronjobs can be run locally via npm package — node-cron. But in Heroku, Jobs scheduled by node_cron won’t run when your free dynos are sleeping, that is if cron execution will be scheduled at the time when your server would be offline, then it won’t work. For this we use Heroku scheduler to perform Cron tasks(We can schedule a task for every 10 minutes or 1 hour or 1 day with the free plan). Here I am taking an example of running an API every 10 minutes.

Running Cron tasks locally using node-cron

Node-Cron is a very simple and powerful npm package from which we can easily schedule tasks of various time periods ranging from seconds to days or months. To start with it locally follow the steps →

  1. npm install cron” in the terminal
  2. Use the following code in your JavaScript file—
var CronJob = require('cron').CronJob;var job = new CronJob('* * * * * *', function() {console.log('You will see this message every second');
// ENTER YOUR TASK HERE
}, null, true, 'America/Los_Angeles');job.start();

3. The asterisk denotes the time period between 2 executions. You can calculate the period using crontab

Checking the task to be executed locally

Calling the API inside the corn function to execute it repetitively. An API can be called using various methods, check all of these here. In the example, I have used Axios to call the API.

Deploying the changes on the Heroku server

Once it is confirmed that the node-cron and task execution are working as expected, we have to make the necessary changes for the Heroku scheduler. If any API is executed in the task, it has to be hosted and cannot be on localhost. This is because the Heroku scheduler creates a separate server and executes the files there.

  1. Create a folder “bin” in the root directory.
  2. Add the JavaScript file inside the ‘bin’ folder ( Ex → bin/Schedulertest.js).
  3. The contents of this javascript file will be executed periodically. So add the task code to this file without the node-cron (any API call for example).
  4. Add, commit and push the changes to the Heroku server.

Scheduling the task on Heroku via Heroku Scheduler

Now we have successfully set up the environment and our code to use Heroku scheduler with Node.js.

Install the Heroku-Scheduler plugin from the terminal(Instructions) or through the website → Application → Resources → Add ons

Heroku dashboard to add “Heroku Scheduler”

Open the Scheduler and add a job with its frequency.

That's it. Yes, you read it correctly, the task scheduling setup is done. Save the job and enjoy the function of the Heroku scheduler.

The working can be confirmed via the Heroku add-on -> One-off Dyno Metrics. Alternatively, we can console something in the file which is to be executed periodically. Open the logs of Heroku on the website or in the terminal using “Heroku logs — — tail” so as to make sure that it is working correctly.

Here is my repository with the basic code and setup. Do raise issues if needed and star if it helped you.

Thanks for reading, hope it helped! :)

More content at plainenglish.io

--

--

Full stack web developer, Freelancer. Some of my hobbies are sports and photography.