【问题标题】:Meteor JS: Background processMeteor JS:后台进程
【发布时间】:2015-09-24 18:14:59
【问题描述】:

我想添加一个函数,该函数将在服务器上每隔几秒钟在一个单独的线程中运行一次。

我阅读了This post 并找到了很多 CRON 包可以帮助我,但我不知道它们的确切位置 API 需要添加到代码中,这可能是因为我不太了解它们如何与 Meteor 一起工作。

我认为我的问题有点生硬,但也许有人告诉我我可以在哪里将 CRON 包功能放入代码中?

【问题讨论】:

    标签: meteor cron


    【解决方案1】:

    现在 SyncedCron 已被弃用,我决定提供一个名为 Steve Jobs 的新后台作业包。 Meteor 开发人员已经过生产测试,易于掌握。

    【讨论】:

      【解决方案2】:

      我使用 https://atmospherejs.com/percolate/synced-cron 作为 cron 作业运行器的示例。

      您将在服务器上运行此包的代码(请参阅:https://github.com/percolatestudio/meteor-synced-cron/blob/master/package.js#L13)。

      例如,您可以在调用 Meteor 方法后安排后台任务:

      Meteor.methods({
        doCron: function() {
          SyncedCron.add({
            name: 'Crunch some important numbers for the marketing department',
            schedule: function(parser) {
              // parser is a later.parse object
              return parser.text('every 2 hours');
            },
            job: function() {
              var numbersCrunched = CrushSomeNumbers();
              return numbersCrunched;
            }
          });
        }
      });
      
      // Somewhere in your code you need this to start processing jobs. Also on server.
      Meteor.startup(function () {
        // code to run on server at startup
        SyncedCron.start();
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2018-04-16
        • 2011-03-31
        相关资源
        最近更新 更多