0

I think this is one of the hardest problems im dealing with in my app, i have installed moment.js (using mrt). and what i want to do is let the user pick a date and a time that the server will send an email to that user friends. so for example: now is thursday 9:13 pm and the user want to send the message monday at 6:47 am. and after the user have set the time i want the email to be sent repeatedly every week at the day and time that the user have set. so i thought using moment.js like that (client side):

var now = moment(); (the current date and time).

then use a datepicker like jqueryui or a common one, and a timepicker,for setting the time and date that the user selected like that(client side):

var day = user selection using datepicker;
var time = user selection using timepicker;
var result = moment.set(day,time);

insert the result to the database:

DateToSendEmail.insert({date:result});

and finally have the code to really execute the Email.send function (on the server side):

var DateToSend = DateToSendEmail.findOne({});
var thisMoment = moment();
if(DateToSend === thisMoment){
Email.send({
to:[friends]
from:"myApplication@xxx.com"
Subject:"auto email send"
html:"auto email send"
});
}

the one thing that i don't know is whether it will work if the user isnt entering the the app for along time (lets say a month) or should i use Meteor.setInterval to execute this function repeatedly?

4

1 回答 1

3

我们在我们的应用程序中做了非常相似的事情:

  1. 将待处理的电子邮件插入Emails带有emailAt日期的集合中。
  2. 在服务器上使用setInterval以定期轮询要发送的电子邮件。

您的投票代码可能类似于:

Meteor.startup(function() {
  if (process.env.METEOR_ENV === 'production') {
    Meteor.setInterval(sendPendingEmails, ONE_HOUR);
  }
});

幸运的是,Date对象是用 UTC 编写的,因此您对它们进行的任何数学运算都应该独立于您的用户或服务器的时区。此外,您可以$lte在从Emails集合中获取时使用,如下所示:

var sendPendingEmails = function() {
  var currentDate = new Date();
  var pendingEmails = Emails.find({emailAt: {$lte: currentDate}}).fetch();
  ...
};

重要的是要记住您的服务器可能会被重置/挂起/等。所以你想设置一个足够小的轮询间隔来弥补这一点,并接近emailAt时间而不会给你的数据库带来太多的负载。

顺便说一句,如果您在操作或比较时刻对象时遇到任何问题,您可能希望toDate()在它们与数据库交互之前调用它们。

于 2013-10-17T18:34:23.037 回答