7

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");
});
Contentop
  • 1,163
  • 3
  • 20
  • 43
  • seems like the scheduler is a add on: https://devcenter.heroku.com/articles/scheduler – split Nov 22 '18 at 02:29
  • BTW changing your timezone on Heroku is only a visual change for the dashboard, I don't believe it actually reflects on the servers. – dotconnor Nov 22 '18 at 02:32
  • This is not the add on scheduler from Heroku. It's a node package on npm – Contentop Nov 22 '18 at 02:36
  • I think you have to check where/how the node package gets its info. I guess from the servers scheduler, and when its not present then.....EDIT: I have just checked - it use cron. – split Nov 22 '18 at 02:39
  • Ok so if uses cron is should work right? – Contentop Nov 22 '18 at 02:48
  • It use some cron-functions: https://github.com/node-schedule/node-schedule/blob/master/lib/schedule.js. Maybe (maybe) a add on of the heroku-scheduler will solve it all. There is VERY little error handling in the "node-schedule-modul" – split Nov 22 '18 at 02:50
  • Do you want to run your function ones every minute ? Then you better use setTimeout() and skip the node-schedule module (and Heroku scheduler) – split Nov 22 '18 at 03:03
  • From original post: "Currently updating every minute just to test it, usefully its once every 1.5 hours" – Contentop Nov 22 '18 at 03:04
  • see: https://stackoverflow.com/questions/30104673/node-js-settimeout-for-24-hours-any-caveats For a 24 h job cron is better but its possible to use setTimeout(). So for 1½ h use setTimeout() and your problem of moving to and from Heroku is solved. – split Nov 22 '18 at 03:11
  • More troubles: You have to let it run every 29 minute to keep Heroku alive: https://stackoverflow.com/questions/40646858/avoid-heroku-server-from-sleeping – split Nov 22 '18 at 03:33
  • That's with a free dyno. I will upgrade to an hobby dyno as soon as my app is released. I'll try setTimout() and also the cron npm package they recommended. – Contentop Nov 22 '18 at 03:37
  • Please tell how it ends. btw: Your needle.put is missing the second (data) parameter. – split Nov 22 '18 at 03:44

1 Answers1

7

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()  
shmit
  • 2,306
  • 2
  • 16
  • 20
  • There is no need to set timezone on Heroku. The job run as per schedule and time zones are as per timezone defined at http://momentjs.com/timezone/ – shmit Nov 22 '18 at 04:23
  • How does this work since heroku powers down idle nodes? – Miguel Stevens Nov 22 '18 at 07:45
  • 1
    Its powers down only those dynos that are running under free tier. You canuse a service like uptime robot to ping those dynos intermittently that will keep them awake but this leads to fast consumption of free 550 dyno hours. – shmit Nov 22 '18 at 21:44
  • I turned to this solution after trying to get `node-schedule` to work to no avail. This works like a charm (first time). – Reece Daniels Jul 04 '20 at 13:58
  • @shmit Thanks for the solution, works great with Heroku! (after failing node-cron & node-schedule) – Ronald Vonk Jan 25 '21 at 13:07
  • 1
    @shmit Thanks for that solution! Saved me a lot of time. Is there any reason that node-cron and node-schedule fail on Heroku but cron does not? – kenneth-rebello Jan 29 '22 at 08:08