sailsjs & laterjs task scheduling, intialization.. poll every x or sleep for x?

I am building an open sourced nodejs powered sprinkler system which will run on the raspberry pi and work with the OpenSprinkler Pi.

I am using:

The end result will be a REST api interface between rpi and whatever front-end you decide to develop for. web/mobile/cli .. etc.

The way the scheduling component will work is values will be posted to the url /programs with a zone id, name, intial status (false), and a plain text human readable schedule phrase such as: every 12 hours starting on the 6th hour before 10 minutes (06:00 & 18:00 for 10 mins).

This schedule will be parsed, and stringified and stored in db as such:

{\"schedules\":[{\"h\":[6,18],\"m_b\":[10]}],\"exceptions\":[],\"error\":-1}

The other values are cleaned and stored as they were entered.

My question is:

how should I go about finding these schedules and determining their run time/date? I've got the scheduling part, and the run time down and working (run for 10 mins). I'm struggling with the theory behind efficiently retrieving the schedules though.


My initial thought was to have a interval every 1 mins poll the db...

setInterval(function(){
  //get programs from db
  //iterate through programs
  //enable programs which start now?
}, 60000);

But this seems kind of illogical. I suppose that when the schedule is created a setInterval or setTimeout is created with the appropriate scheduling info.. but what happens if the rpi loses power, or those could eat up a bit of memory having all of these intervals hanging out there..

  • how will it handle existing schedules already in the db?

open source repo

of what i have so far (not much other than the creation api/models/Programs.js) is here: https://github.com/RelativeMedia/nodesprinkler.git