Hi there.
I need to run a function at the beginning of each day (to add extra vacation time to an employee’s balance) and I wanted to share what I’ve come up with.
Here’s how I plan everything to work:
- Get the diff between now and the next interval
- setTimeout() to wait until the next interval
- Run daily code once when setTimeout() expires (or the first run will be missed)
- setInterval() to wait 24 hours
- Clear setTimeout()
- Let setInterval() run indefinitely
(Discourse wants a paragraph here or else it won’t render the code properly.)
Meteor.startup(function () {
// get current Moment(), round up to the next day at 12AM, get diff between
// now and that time
var waitTillMorning = moment().startOf('day').add(1, 'd').diff(moment());
var millisPerDay = 86400000;
var timeout = Meteor.setTimeout(function () {
console.log('timeout started');
Meteor.setInterval(function () {
console.log('interval executed');
}, millisPerDay);
Meteor.clearTimeout(timeout);
}, waitTillMorning);
});
Is this the right way to go about this? A few things I’m not sure of:
- Is Meteor.clearTimeout() necessary here?
- Is this method safe to run for an extended period of time? Or should I plan to restart the Meteor process on an interval (weekly?) at the server?
Thanks.