Andrew
Andrew

Reputation: 3989

How to run a Parse cloud job in batches of users to avoid time limit?

I'm currently running the following Parse cloud code job which iterates through every user in the database, and I'm starting to hit the 15 minute time limit. How can I set this to run in batches of 500 users at a time instead of all users at once? I would need it to run through the users in order, so user 1-500 for the first batch, then 500-1000 for the 2nd batch, and so on, that way it doesn't repeat anyone.

Parse.Cloud.job("MCBackground", function(request, status) {
    // ... other code to setup usersQuery ...
    Parse.Cloud.useMasterKey();

    var usersQuery = new Parse.Query(Parse.User);

    return usersQuery.each(function(user) {
            return processUser(user)
                .then(function(eBayResults) {
                    return mcComparison(user, eBayResults);
                });
        })
        .then(function() {
            // Set the job's success status
            status.success("MCBackground completed successfully.");
        }, function(error) {
            // Set the job's error status
            status.error("Got an error " + JSON.stringify(error));
        });
});

Upvotes: 1

Views: 108

Answers (1)

Marius Waldal
Marius Waldal

Reputation: 9942

Well, for starters, from your comment I see you have a constraint you can add: only query for user objects that has a matchCenterItem:

query.exist("matchCenterItem");

I don't know how many percent of your users would not have this field, but maybe you already now will have reduced the number of Users to fetch.

I have no idea how often this job is run or what you are comparing with, but how likely is it that this data will change? Do you always need to run it on ALL users, or can you set a flag so that you don't run it on the same user again? At all, or until some time has passed?

Upvotes: 0

Related Questions