javascript - 实现渗透 :synced-cron in meteor, 以安排插入到集合中

标签 javascript meteor cron meteor-collections

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

TLDR:用户插入数据,用户选择发布日期,synced-cron 将数据存储在 cronHistory 集合中与用户选择的日期。到达日期时,相同的数据将插入到 Posts 集合中。

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

谢谢!提前寻求任何帮助。

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

**客户端**

/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/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.

更新尝试新代码。

这是订阅所有集合时的帖子示例。

{
  "_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 时,它没有返回任何内容,但是,如果我为 future 它也会输出。我不确定我可能做错了什么,也许这是我的 timeToEnable 日期的格式?

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

最佳答案

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

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

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

在您的帖子中添加 timeToEnable 字段:

 {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
}

然后在您的服务器代码中发布帖子集合,并通过查询查找所有带有 timeToEnable 的帖子

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

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

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

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

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

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

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

关于javascript - 实现渗透 :synced-cron in meteor, 以安排插入到集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30336863/

相关文章:

php - 在 Windows 服务器上运行 "cron"作业

javascript - 将 PHP 生成的 JS 对象传递给 JS

javascript - $rootScope 未使用 forEach 进行更新

javascript - 无法在phonegap中使用googlemaps进行地理定位

javascript - 有什么方法可以调试 meteor Jasmine 客户端单元测试吗?

javascript - 在下拉更改事件上绑定(bind) aldded 表格中的数据

linux - Codeigniter cron 作业问题

java - 如何配置log4j只保留最近n天的日志文件?

javascript - 关于http请求中的流式传输的问题

javascript - Meteor 服务器端控制台错误