3

I am using Cloud Code to update all users, everyday. It used to work, but now getting error after 5 minute processing. "the service is currently unavailable" without any reason. I have checked status.parse.com and there is no relevant down. I have 10 000 users.

Parse.Cloud.job("makeUsersPassiveAndSendPushes", function(request, status) {

Parse.Cloud.useMasterKey();

var activeUsers = [];
var limitDoneUsers = [];
var nowDate=new Date();
var updatedUsers = [];

var query = new Parse.Query(Parse.User);
query.equalTo("passive",false);
query.each(function(user) {

  if(user.get("passive") === false){
    activeUsers.push(user);
    user.set("passive", true);
    user.set("passiveDate",nowDate);
  }

  if(user.get("isLimitDone")){
    limitDoneUsers.push(user);
  }

  user.set("isLimitDone",false);
  user.set("activeMatch",null);
  user.set("canGetMatch",true);
  user.set("dailyMatchEndCount",0);
  //user.set("lastMatchLimit",false);
  user.set("todaysMatches",[]);

  updatedUsers.push(user);

  return user.save();
})

Could you help me? Thanks.

Emre
  • 295
  • 2
  • 9

1 Answers1

0

You may want to try modifying the last line from:

return user.save();

to use callbacks for the save function, to ensure they are firing in sequence:

return user.save(null, {
                success: function (user) {
                    return user;
                },
                error: function (error) {
                    return Parse.Promise.error(new Error("error"));
                }
            });

Another alternative would be to use the saveAll function like this:

return Parse.Object.saveAll(updatedUsers).then(function() {
                    //code that fires after all objects are saved
                });

Also, are you using the hosted Parse.com environment or have you transitioned to another provider like Heroku & mLab?

As a fellow Parse user with this same issue (background job failing with this error when performing many inserts), I look forward to any comments you may have.