【问题标题】:How is everyone going about implementing scheduled jobs / cloud jobs on parse-server?每个人都如何在解析服务器上实现计划作业/云作业?
【发布时间】:2017-01-06 03:49:21
【问题描述】:

根据解析服务器迁移指南,我们可以使用 KueKue-UI 之类的东西来模拟 parse.com 计划作业功能。

我还没有实现 Kue 或 Kue-ui,但查看指南,它看起来不像提供与现有 parse.com 计划作业相同级别的功能。这个观察正确吗?有人实施了吗?是不是必须在javascript中通过Kue调度作业,而Kue-ui只提供作业当前状态的摘要,而不能通过Kue-ui添加新的调度?

有没有人尝试过使用像 Jenkins 这样的工具来达到相同的结果?所以这就是我的想法:

  • 每个作业仍将在云代码中定义 Parse.Cloud.job("job01", function(request, response) {));
  • 稍微修改 parse-server 以在与现有云功能类似的 url 处公开作业,例如/parse/jobs/job01(这可能很快就会出现在 parse-server 中:github.com/ParsePlatform/parse-server/pull/2560)
  • 创建一个新的 jenkins 作业,在该 url 上做一个 curl
  • 在 jenkins web ui 中为 jenkins 作业定义类似 cron 的计划

我可以看到以下好处:

  • 几乎没有编码
  • 设置 jenkins 听起来比设置 kue、redis 和 kue-ui 的工作要少得多
  • 现有的云作业/定义保持不变
  • 通过 jenkins web ui 安排和手动触发作业

当前 parse.com 计划作业/云作业唯一能做的事情是基于 jenkins 的解决方案无法从下拉列表中选择一个作业名称来创建一个新的计划。

我错过了什么吗?其他人是怎么做的?谢谢。

【问题讨论】:

标签: jobs parse-server kue


【解决方案1】:

我最终使用“node-schedule”来完成这项工作。不确定这是否是最佳选择,但对我来说效果很好。

index.js

var schedule = require('node-schedule');
var request = require('request');
schedule.scheduleJob('*/15 * * * *', function() {
    var options = {
        url: serverUrl + '/functions/{function name}}',
        headers: {
            'X-Parse-Application-Id': appID,
            'X-Parse-Master-Key': masterKey,
            'Content-Type': 'application/json'
        }
    };
    request.post(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    });
});

ma​​in.js

Parse.Cloud.define('{function name}', function(req, res) {
  //Check for master key to prevent users to call this 
  if (req.master === true) {
    //Do operations here
  } else {
    res.error('Need Master Key');
  }
});

【讨论】:

    【解决方案2】:

    我为此使用kue。我已经描述了方法in my article。简而言之,这个函数:

    Parse.Cloud.job("sendReport", function(request, response) {
      Parse.Cloud.httpRequest({
      method: 'POST',
      headers: {
       'Content-Type': 'application/json',
      },
      url: "https://example.com/url/", // Webhook url
      body: "body goes here",
      success: function(httpResponse) {
          console.log("Successfully POSTed to the webhook");
          },
      error: function(httpResponse) {
          console.error("Couldn't POST to webhook: " + httpResponse);
          }
      });
    });
    

    变成这样:

    // Create a kue instance and a queue.
    var kue = require('kue-scheduler');
    var Queue = kue.createQueue();
    var jobName = "sendReport";
    
    // Create a job instance in the queue.
    var job = Queue
                .createJob(jobName)
                // Priority can be 'low', 'normal', 'medium', 'high' and 'critical'
                .priority('normal')
                // We don't want to keep the job in memory after it's completed.
                .removeOnComplete(true);
    
    // Schedule it to run every 60 minutes. Function every(interval, job) accepts interval in either a human-interval String format or a cron String format.
    Queue.every('60 minutes', job);
    
    // Processing a scheduled job.
    Queue.process(jobName, sendReport);
    
    // The body of job goes here.
    function sendReport(job, done) { 
      Parse.Cloud.httpRequest({
      method: 'POST',
      headers: {
       'Content-Type': 'application/json',
      },
      url: "https://example.com/url/", // Webhook url
      body: "body goes here"}).then(function(httpResponse) {
        console.log("Successfully POSTed to the webhook");
        // Don't forget to run done() when job is done.
        done();
      }, function(httpResponse) {
        var errorMessage = "Couldn't POST to webhook: " + httpResponse;
        console.error(errorMessage);
        // Pass Error object to done() to mark this job as failed.
        done(new Error(errorMessage));
      });
    }
    

    它工作正常,但我注意到有时kue-scheduler 会比需要的更频繁地触发事件。有关更多信息,请参阅此问题:https://github.com/lykmapipo/kue-scheduler/issues/45

    【讨论】:

    • 嗨@andrey-gordeev。感谢您的反馈意见。尽管您没有直接指出,但我得出的结论是否正确,您不能通过任何 kue、kue-ui 或 kue-scheduler 组合通过某种 UI 安排作业?谢谢。
    • @asdf01 kue 有一个单独的UI组件,但我没试过。
    • 如果我改用 kue-scheduler,Parse.Cloud.job 有什么用?
    【解决方案3】:

    如果您在 AWS 上,这可能是一个选项:

    1. 创建以特定时间间隔触发并调用 Lambda 函数的 AWS CloudWatch 事件规则。事件规则可以将参数传递给 Lambda 函数。

    2. 创建一个调用云代码函数/作业的简单 Lambda 函数。如果您从事件规则中收到云代码函数名称和其他参数,则任何云代码调用只需要一个通用 Lambda 函数。

    这有几个优点,因为事件规则是 AWS 基础设施的一部分,可以轻松与其他 AWS 服务集成。例如,您可以设置 Event Rule 调用的智能排队,这样如果之前的调用尚未完成,则丢弃队列中的下一个调用、溢出到另一个队列、通知操作员等。

    【讨论】:

      【解决方案4】:

      您可以使用parse-server-scheduler npm 模块。

      它不需要任何外部服务器,只允许您在 parse-dashboard 中设置调度。

      【讨论】:

        猜你喜欢
        • 2020-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多