Running Cron tasks on Heroku for free using 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._
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 →
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
#javascript #nodejs #software-development #heroku