Meteor:每天使用 setInterval() 运行 函数一次
Meteor: Using setInterval() To Run Function Once Per Day
我正在尝试 运行 一个将搜索特定参数并每天仅向用户发送电子邮件一次的功能。
我正在寻找许多可以完成的方法。 setInterval() 似乎是一种方法。
setInterval(function () {
var date = new Date();
if (date.getDate() === 12 && date.getHours() === 10 && date.getMinutes === 0) {
alert("Surprise!!")
}
}, 1000)
例如,上面的内容会在每天上午 10 点触发。
我还发现有一个包 运行s cron jobs。
https://github.com/percolatestudio/meteor-synced-cron
看来我可以设置一个cron 作业来每天发送电子邮件。我从未使用过 cron 作业,所以我认为此时此选项对我来说要困难得多。
最后,我也找到了这个方法:
Call function once on day change
只有一个常规函数。
似乎 setTimeout() 是完成它的最简单方法。但是有什么挫折吗?我不想错过电子邮件,或者更糟的是多次错过电子邮件用户。我的网站已经上线并在不断发展,所以我不想费力地找出答案。
感谢任何帮助。
我认为您必须使用 Meteor.setInterval()
而不是 setInterval()
才能使其在服务器上正常工作。但是,我将它用于每 秒 需要 运行 的功能;对于更长的时间,任何类型的 cron 作业包也可以正常工作,例如 synced-cron
.
我建议使用 percolate:synced-cron 等软件包来实现此目的。
所以,完成这个主题,也许可以帮助处于相同情况的任何人。我就是这样做的。我添加了包 'percolate:synced-cron'。然后在服务器端我创建了一个文件'cron.js'。这是我使用的代码。为了保护隐私,我删除了功能的其余操作部分,但重要的部分都在这里。 parser.text 使得选择时间变得非常容易。 'every 5 seconds'(每 5 秒执行一次操作).. 或 ('at 10 am') 将在每天上午 10 点执行操作。请记住,mongo 设置为标准 UTC 时间,因此您必须进行转换。
if (Meteor.isServer) {
// optionally set the collection's name that synced cron will use
SyncedCron.config({
collectionName: 'somethingDifferent'
});
SyncedCron.add({
name: 'Crunch some important numbers for the marketing department',
schedule: function(parser) {
// parser is a later.parse object
// return parser.text('every 5 seconds');
return parser.text('at 3:00 am');
// midnight is at 5pm LA time ... 10 am LA time is 3 am UTC
},
job: function(intendedAt) {
var today = new Date();
var yesterday = new Date()
var dayBeforeYesterday = new Date()
yesterday.setDate(today.getDate() - 1)
dayBeforeYesterday.setDate(today.getDate() - 2)
var todaysUsers = Meteor.users.find({ createdAt: { $lt: (yesterday), $gt: dayBeforeYesterday } }).fetch()
//rest of function here
}
}
});
Meteor.startup(function () {
// code to run on server at startup
SyncedCron.start();
// Stop jobs after 15 seconds
// Meteor.setTimeout(function() { SyncedCron.stop(); }, 7 * 1000);
});
}
我正在尝试 运行 一个将搜索特定参数并每天仅向用户发送电子邮件一次的功能。
我正在寻找许多可以完成的方法。 setInterval() 似乎是一种方法。
setInterval(function () {
var date = new Date();
if (date.getDate() === 12 && date.getHours() === 10 && date.getMinutes === 0) {
alert("Surprise!!")
}
}, 1000)
例如,上面的内容会在每天上午 10 点触发。
我还发现有一个包 运行s cron jobs。 https://github.com/percolatestudio/meteor-synced-cron
看来我可以设置一个cron 作业来每天发送电子邮件。我从未使用过 cron 作业,所以我认为此时此选项对我来说要困难得多。
最后,我也找到了这个方法: Call function once on day change
只有一个常规函数。
似乎 setTimeout() 是完成它的最简单方法。但是有什么挫折吗?我不想错过电子邮件,或者更糟的是多次错过电子邮件用户。我的网站已经上线并在不断发展,所以我不想费力地找出答案。
感谢任何帮助。
我认为您必须使用 Meteor.setInterval()
而不是 setInterval()
才能使其在服务器上正常工作。但是,我将它用于每 秒 需要 运行 的功能;对于更长的时间,任何类型的 cron 作业包也可以正常工作,例如 synced-cron
.
我建议使用 percolate:synced-cron 等软件包来实现此目的。
所以,完成这个主题,也许可以帮助处于相同情况的任何人。我就是这样做的。我添加了包 'percolate:synced-cron'。然后在服务器端我创建了一个文件'cron.js'。这是我使用的代码。为了保护隐私,我删除了功能的其余操作部分,但重要的部分都在这里。 parser.text 使得选择时间变得非常容易。 'every 5 seconds'(每 5 秒执行一次操作).. 或 ('at 10 am') 将在每天上午 10 点执行操作。请记住,mongo 设置为标准 UTC 时间,因此您必须进行转换。
if (Meteor.isServer) {
// optionally set the collection's name that synced cron will use
SyncedCron.config({
collectionName: 'somethingDifferent'
});
SyncedCron.add({
name: 'Crunch some important numbers for the marketing department',
schedule: function(parser) {
// parser is a later.parse object
// return parser.text('every 5 seconds');
return parser.text('at 3:00 am');
// midnight is at 5pm LA time ... 10 am LA time is 3 am UTC
},
job: function(intendedAt) {
var today = new Date();
var yesterday = new Date()
var dayBeforeYesterday = new Date()
yesterday.setDate(today.getDate() - 1)
dayBeforeYesterday.setDate(today.getDate() - 2)
var todaysUsers = Meteor.users.find({ createdAt: { $lt: (yesterday), $gt: dayBeforeYesterday } }).fetch()
//rest of function here
}
}
});
Meteor.startup(function () {
// code to run on server at startup
SyncedCron.start();
// Stop jobs after 15 seconds
// Meteor.setTimeout(function() { SyncedCron.stop(); }, 7 * 1000);
});
}