在 meteor 中实施 percolate:synced-cron,以便安排插入 collection

Implementing percolate:synced-cron in meteor, in order to a schedule a insert into a collection

有人建议我使用 thissynced-cron 来安排代码的执行。我有以下代码,当时什么都不做。我的概念是,在设置计划时使用此包会创建 collection 并存储传递到计划中的所有数据,直到将来在指定时间您可以在不同的函数中执行相同的代码,在我的例子中,它将插入到 Posts collection 中,然后显示在模板上。

TLDR: 用户插入数据,用户选择日期post,synced-cron将数据存储在cronHistory collection 以及用户选择的日期。到达日期时,将相同的数据插入 Posts collection.

我的假设是,这是我对编程的部分了解,而不了解基础知识的结果。因此,如果有人对学习核心概念有任何建议,我也持开放态度,因为我认为我浪费了很多时间来提问,而这实际上是我所缺少的基本概念。

谢谢!提前寻求帮助。

这是我的文件夹结构代码。

**client**

/client/posts/post_item.js

Template.postItem.rendered = function(){

  var post = {
    title: "testTitleTime",
    postContent: "timeContentRest"
  };
  var time = "at 2:30 pm";
  Meteor.call('schedulePosts',post,time, function(error, result) {});

};

So my theory here is that data is passed to the meteor method schedulePosts which is on the server. My intention is to have it stored in what the documentation says is a collection called cronHistory. Although I may be completely misinterpreting what cronHistory is.

**lib**

/lib/posts.js

Meteor.methods({
  postInsert: function(postAttributes) {
    check(Meteor.userId(), String);
    check(postAttributes, {
      title: String,
      postContent: String

    });
    var user = Meteor.user();
    var post = _.extend(postAttributes, {
      userId: user._id,
      author: user.username,
      submitted: new Date()
    });
    var postId = Posts.insert(post);
    return {
      _id: postId
    };
  }
});

I know this code works if the correct attributes are passed to it so I am not to worried about it, but it needs to work in flow with everything else.

**server**

/server/schedule.js

Meteor.methods({
  schedulePosts: function (time, post) {

  SyncedCron.add({
    name: 'Crunch some important numbers for the marketing department',
    schedule: function(parser) {
      // parser is a later.parse object
      return parser.text(time);
    },
    job: function() {
      Meteor.call('postInsert', post, function(error, result) {});
      }

  });

  var MyLogger = function(opts) {
    console.log('Message', opts.message);
  };

  SyncedCron.start();

 }

});

What I think is happening here is the method is run, the schedule is initialized based on the data passed from post_item.js, once that time is reached the job function is run and the post data from post_item.js is inserted.

更新尝试新代码。

这里是一个例子post当所有的collection都被订阅了。

{
  "_id": "jgx5qWf4RoeG6u7KJ",
  "title": "1 20",
  "postContent": "See if it w",
  "timeToEnable": "2015-06-20T20:20:00.000Z",
  "userId": "t4Y9hqYQCG9gxDgnW",
  "author": "admin",
  "submitted": "2015-05-20T20:19:27.603Z"
}

当我尝试在下面使用它时,当我将它从 $lt 更改为 $gt 时,没有任何返回 returns 但是,如果我安排 post对于未来,它也会输出。我不确定我可能做错了什么,也许这是我的 timeToEnable 日期的格式?

Meteor.publish('posts', function() {
    var selector = {timeToEnable: {$lt: new Date()}};
    return Posts.find(selector);
});

我认为 syncedCron 不适合您的工作 - 您希望在将来的预定时间做一次某事。而 cron 作业是您希望在固定时间、每 2 小时或每周三午夜定期一次又一次地执行的作业。

您可能会忘记 syncedCron 并让 meteor 为您做这件事,因为 Meteor 是反应式的。

立即将 post 放入 Posts 集合,因为稍后找到一条记录并将其再次写入 posts 集合是一件很复杂的事情。

将 timeToEnable 字段添加到您的 posts:

 {title: String,
  postContent: String
  user: Meteor.user();
  userId: user._id,
  author: user.username,
  submitted: new Date()
  timeToEnable: new Date(year, month, day, hours, minutes, seconds, milliseconds
}

然后在您的服务器代码中发布 posts 集合,使用查询查找所有 posts with timeToEnable < now

Meteor.publish('posts', function() {
    var selector = {timeToEnable: {$lt: new Date()}};
    return Posts.find(selector);
});

然后在客户端上订阅该出版物,并以通常的方式在 Blaze 模板中显示 post 的列表。

我相信就这么简单:这就是 Meteor 的意义所在。 您的 ui 将自行更新。

我在这里创建了一个meteorpad应用程序:

http://meteorpad.com/pad/EpAmSL68hZ7L3ig5j/futurePost

您需要将 reactive-dict 添加到您的应用程序,因为 Meteor 会对数据库更改做出反应,而我们需要对时间更改做出反应 - 如果没有 reactive-dict,这对 Meteor 来说毫无意义。

每次页面刷新(在 template.onCreated() 中)时,meteor pad 应用程序都会调用一个服务器方法,该方法创建六个记录,每五秒有一个 timeToEnable。稍等一下,您会看到每五秒出现一条记录。