I encountered a problem with my CountDown. I tried to send emails to some people i inserted in my collection before, when the counter expires (t<=0)
. The problem is, its only getting fired when iam visiting the page. Is it anyhow possible that the server sends the emails when even noone is on the client side? I expect that i have to build something on the server side?
Template.decision.onRendered(function(){
clearInterval(timeinterval);
timeinterval = setInterval(function () {
var endtime = '2016/02/10'
Meteor.call("getCurrentTime", function (error, result) {
Session.set("time", result);
var t = getTimeRemaining(endtime);
Session.set("t", t);
});
}, 1000);
});
function getTimeRemaining(endtime){
var t = Date.parse(endtime) - Session.get('time');
var seconds = ("0" + Math.floor( (t/1000) % 60 )).slice(-2);
var minutes = ("0" + Math.floor( (t/1000/60) % 60 )).slice(-2);
var hours = ("0" + Math.floor( (t/(1000*60*60)) % 24 )).slice(-2);
var days = Math.floor( t/(1000*60*60*24) );
console.log(t);
if(t <= 0 && timeinterval) {
clearInterval(timeinterval);
var to = Questions.findOne({_id:selectedDecisionId}).email;
var questionText = Questions.findOne({_id:selectedDecisionId}).questionDB;
var nameCreater = Questions.findOne({_id:selectedDecisionId}).name;
Meteor.call('sendEmail',to,questionText,nameCreater);
}
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
And on my server:
Meteor.methods({
'getCurrentTime': function (){
return Date.parse(new Date());
}
});
The better way to approach this is by using a cron job on the server. This is easily doable using the percolate:synced-cron package.
SyncedCron.add({
name: 'Send emails',
schedule: function(parser) {
// parser is a later.parse object
return parser.text('every 1 minute');
},
job: function() {
// find relevant, expired documents
// send an email to each user that is affected
}
});