node-schedule / node-schedule

A cron-like and not-cron-like job scheduler for Node.
MIT License
9.11k stars 513 forks source link

Scheduled Jobs dont always execute on raspbian (Raspberry Pi 3) #376

Closed jfarago closed 6 years ago

jfarago commented 7 years ago

I have been using the scheduler for a few months now and have an issue where the jobs will not always run.

Im running an express server with this scheduler and when a job doesnt run i hit up my web page to manipulate it manually and the moment the page loads, the job runs. Not sure where to start debugging this.

Not all scheduled jobs seem to be affected.

config.json

{
  "pins": [
    {
      "pinNumber": 6,
      "direction": "out",
      "description": "White Lights",
      "schedule": {
        "on": "12",
        "off": "18"
      }
    },
    {
      "pinNumber": 5,
      "direction": "out",
      "description": "Blue Lights",
      "schedule": {
        "on": "10",
        "off": "20"
      }
    },
    {
      "pinNumber": 21,
      "direction": "out",
      "description": "Refugium Lights",
      "schedule": {
        "on": "21",
        "off": "9"
      }
    },
    {
      "pinNumber": 12,
      "direction": "out",
      "description": "Protein Skimmer"
    },
    {
      "pinNumber": 20,
      "direction": "out",
      "description": "Return Pump"
    },
    {
      "pinNumber": 13,
      "direction": "out",
      "description": "Power Head"
    },
    {
      "pinNumber": 19,
      "direction": "out",
      "description": "Heater"
    },
    {
      "pinNumber": 16,
      "direction": "out",
      "description": "Auto Top Off"
    }
  ]
}

scheduler.js

const schedule = require('node-schedule');

var currentJobs = [];

exports
  .createSchedule = createSchedule;

console.log("Current time is: ", new Date());

function createSchedule(config, outlets) {
  for (var i = 0; i < config.length; i++) {
    if (config[i].schedule) {
      if (config[i].schedule.on && config[i].schedule.off) {
        //create closure to have access later
        (function(outlet, config) {
            newJob(config.schedule.on, function() {
              console.log('Setting ' + outlet.description + ' on from scheduler ' + new Date());
              outlet.set(1);
            });
            newJob(config.schedule.off, function() {
              console.log('Setting ' + outlet.description + ' off from scheduler ' + new Date());
              outlet.set(0);
            });
        })(outlets[i], config[i]);
      } else {
        console.log("Error: Ensure you have on and off times")
      }
    }
  }
  return currentJobs;
}

function newJob(hour, callback) {
  var rule = new schedule.RecurrenceRule();

  rule.hour = parseFloat(hour);
  rule.minute = 0; //comment out to run every minute on the hour specified.

  var job = schedule.scheduleJob(rule, callback);

  currentJobs.push(job);
}
santigimeno commented 7 years ago

There have been issues with nodejs timers on rpi. See: https://github.com/node-schedule/node-schedule/issues/218, https://github.com/nodejs/node/issues/4262. Apparently some of them should be fixed on v8.1.2, can you test with that?

jfarago commented 7 years ago

@santigimeno i will give that a shot, thanks.

jfarago commented 6 years ago

@santigimeno Unfortunately, I could not update node as I am on a pi zero but I was able to set a 5 minute interval to act as a keep alive. Thanks for the help.

santigimeno commented 6 years ago

I'm going to close this. Please reopen if you think it should be