限速parse.com后台工作 [英] Rate limiting parse.com background jobs

查看:92
本文介绍了限速parse.com后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在parse.com上有一些需要由后台作业更新的数据。大约有100行。更新每一行使用解析api的3个查询。我每秒只允许30次查询,因此每次运行作业时只会更新前9行,并且在该秒期间碰巧访问系统的任何用户都会收到错误。

I have some data in parse.com that needs to be updated by a background job. There are around 100 hundred rows. Updating each row uses 3 queries of the parse api. I am only allowed 30 queries a second, so only the first 9 rows get updated each time I run the job, and any users who happen to access the system during that second will get errors.

是否有一种简单的方法可以在作业中暂停,以便它只在200ms左右后更新一行,然后作业只使用一半可用的查询?这意味着工作大约需要20秒才能运行,而不是仅仅一秒钟,但这是一个完全可以接受的权衡。

Is there a simple way to put pauses in the job so that it only updates a row after 200ms or so, and the job then only uses half the queries available? It means the job will take about 20 seconds to run, rather than just one second, but that's a totally acceptable tradeoff.

代码的结构大致如下:

Parse.Cloud.job("UpdateData", function (request, status) {
  Parse.Cloud.useMasterKey();

  (new Parse.Query("Table"))
    .find()
    .then(function (rows) {
      rows.forEach(function (row) {
        // Some queries happen here
        ...
        row
          .set(...)
          .save();
      });
      status.success();
    }, function (error) {
      status.error();
    });
});

我认为我不能使用 setTimeout setInterval 可以轻松地使用此代码(我甚至不确定他们是否允许使用parse.com云代码)。 JS不支持任何类似 sleep() wait()

I don't think I can use setTimeout or setInterval easily on this code (I'm not even sure if they are allowed on parse.com cloud code). JS doesn't support anything like sleep() or wait().

推荐答案

尝试 Parse.Object.saveAll 一次保存所有对象,也可以链接Parse promises in series如果在你的forEach循环中需要其他查询,请提及@ mbm29414。

Try Parse.Object.saveAll to save all object at once and also to chain Parse promises in a series as @mbm29414 mention if you need other queries in your forEach loop.

以下代码片段显示了一个使用 Parse一次保存对象的简单示例.Object.saveAll

The following code snippet shows a simple example of saving object at once with Parse.Object.saveAll:

Parse.Cloud.job("UpdateData", function (request, status) {
  Parse.Cloud.useMasterKey();

  (new Parse.Query("Table"))
    .find()
    .then(function (rows) {
      var toSaveList = [];
      var promise = new Parse.Promise();
      rows.forEach(function (row) {
        // Some queries happen here
        ...
        row.set(...);
        ...
        toSaveList.push(row)
      });
      Parse.Object.saveAll(toSaveList, function() {
        promise.resolve();
      }, function() {
        promise.reject();
      });
      return promise;
    })
    .then(function() {
      status.success();
    }, function() {
      status.error();
    });
});

这篇关于限速parse.com后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆