I have an application in which a user can manage multiple alarms (set a new alarm, edit it, or snooze)
When the alarm time is reached I need to notify the user. If a user clicks snooze, the time_end field gets 5 minutes added to it.
How can I track when the alarm times have been reached?
I have tried using a collection.observe()
- but it only works once, on server start
Server: Meteor Startup
var expiredAlarms = Alarms.find({$and: [{"time_end": {$lt: moment()._d}}, {notification_sent: false}]});
expiredAlarms.observe({
added: function(doc) {
console.log('alarm timeout has been reached');
processAlarmEnd(); //set notification_sent to true
},
removed: function(doc) {
console.log('A notification has been sent');
}
});
The above code only works when the app is started and processes all notifications for expired alarms - but once new alarms expire, nothing happens. My guess is because moment()._d
does not change and the same old query is called over and over again.
Should I place this code in a Tracker.autorun
- will that help? Any suggestions on doing this better/smarter?