Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make node-schedule work in Heroku?

I am running jobs from the 'node-schedule' module.

On localhost everything works great but when I upload to production in Heroku it doesn't.

i have changed my timezone in the settings -> var config to TZ at Asia/Jerusalem but it still doesn't work. Any idea why? Uploading my code although I think it is something with Heroku, not the code. Currently updating every minute just to test it, usefully its once every 1.5 hours

const schedule = require("node-schedule");
const needle = require("needle");

let j = schedule.scheduleJob("* /1 * * * *", function() {
    needle.put("https://myserver.herokuapp.com/myendpoint");
});
like image 616
Contentop Avatar asked Nov 22 '18 02:11

Contentop


People also ask

Does node schedule work on Heroku?

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.

How do I run a scheduler on Heroku?

Installing the add-onNavigate to the “Resources” tab of the app's Dashboard. Search for “Heroku Scheduler” in the Add-ons search box. Follow the prompts to provision the Add-on.

Does cron work on Heroku?

Cron To Go is an add-on created for Heroku users to run scheduled jobs based on one-off dynos using cron expressions. Cron To Go combines the simplicity of using cron to schedule jobs with the scale and elasticity of the cloud.


1 Answers

I am successfully using cron jobs on Heroku and Azure with following code. I am using cron

import { CronJob } from 'cron';

  const doSomething = new CronJob(
    '0 0 * * 1', //cron time
    fnname, //replace with your function that you want to call
    null, //oncomplete
    false, //start flag
    'America/Los_Angeles',// timezone
  );
  
  doSomething.start()  
like image 74
shmit Avatar answered Oct 28 '22 12:10

shmit