Cron工作在Meteor
我想知道是否可以在任何特定的时间删除或修改MongoDB集合中的数据。
把它看作是在一天结束时必须删除/更新的微不足道的数据。 把我指向一个方向。
Meteor有两个很好的select做cron工作:
- https://github.com/percolatestudio/meteor-synced-cron
- https://github.com/vsivsi/meteor-job-collection
根据我的经验,如果你的用例很简单,你需要一个快速,轻量级的解决schememeteor-synced-cron应该没问题。 如果你的任务更复杂,你需要更多的工作控制,然后去工作收集
对于meteor,我没有像cron
那样的标准节点软件包能够正常工作,并显示出使用Fibers的消息。 对于一个简单的日常任务,我创build了一个直接使用Meteor.setTimeout()的函数。 这样它将保持Meteor环境可用,所以你可以做你每天数据库清理。
它稍后使用节点包来为计划何时启动'cron'作业。 您可以使用要调用的函数的名称replaceyourDailyCleanup。
import { Meteor } from 'meteor/meteor'; import later from 'later'; function scheduleTimeout(sched, fn) { const nowMilli = Date.now(); const next = later.schedule(sched).next(1,nowMilli+1001); console.log('next schedule',next); const diffMile = next.getTime() - nowMilli; Meteor.setTimeout( function() { scheduleTimeout(sched,fn); fn(); } , diffMile ); } Meteor.startup(function() { console.log('Startup'); later.date.localTime(); // scheduleTimeout( later.parse.recur().every(2).minute(), function() { console.log('test job');} ); scheduleTimeout( later.parse.recur().on('23:00:00').time(), yourDailyCleanup ); });
代码基于percolatestudio软件包:meteor-synced-cron ,当你需要更多的function时,你也可以使用它。