0

In my parse server I have a class called Stats which contains the column timeScore (number)

I am using cloud code to update all 250k rows in the column timeScore.

My code works well except for the fact that I get the following H12 - Request timeout error for no apparent reason.

heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/functions/timeScore

I get the timeout error regardless of what batchSize I choose and I get it every 30 seconds. I get it for a total of 5 times. After the 5th time I dont get it anymore and the process continues and finishes without any issues or errors. I get the 5th and last error at around 2.5 minutes into the process (30seconds*5).The whole process takes about 14 minutes. These timeout errors does not effect the process in any way. All 250k objects are updated and saved regardless of the selected batchSize and despite the timeout errors.

I thought maybe because I never call results.error or results.success the server thinks I am still doing some work and shows the error. However I updated my code as seen below to include them and I still get the timeout errors.

The problem is after each timeout error processBatch() is called once again from the beggining. Since I get 5 timeout errors processBatch() gets called 5 times. So after the 5th timeout error there are 5 processBatch() functions running simultaneously (I confirmed this with logs). This makes the process take much longer that it should take.

I am not performing any long running actions. I am just updating and saving data on my parse server.

Here is my code:

var _ = require("underscore");
Parse.Cloud.define("timeScore", function(request, response) {
var counter = 0;
function processBatch(query, batchSize, startingAt, process) {
    query.limit(batchSize);
    query.skip(startingAt);

    return query.find().then(results => {

        return process(results).then(() => results.length);
    }).then(length => {

        return (length === batchSize)? processBatch(query, batchSize, startingAt+length, process) : {};
    });
}

function setTimeScores(stats) {
        console.log("LENGTH " + stats.length);
    _.each(stats, stat => {

        counter ++;
        stat.set("timeScore", counter);

    });
    return Parse.Object.saveAll(stats);
}

var query = new Parse.Query("Stats");

processBatch(query, 1000, 0, setTimeScores).then(results => {
        response.success(results);
    }).catch(error => {
        response.error(error);
    });

});

Here is how I call it

#!/usr/bin/env node
var Parse = require("parse/node");
Parse.initialize("xx",   "xx");
Parse.serverURL = "http://randomapp.herokuapp.com/parse";
Parse.Cloud.run('timeScore');

What is causing the heroku timeout errors I am getting? How do I fix it?

Murat Kaya
  • 193
  • 1
  • 18
  • curious about the recursion in 'processBatch' . You could investigate use of 'workers' to bypass the H12 .. https://github.com/heroku-examples/node-articles-nlp – Robert Rowntree Jul 17 '18 at 15:52
  • 'increment' may also be useful, atomic OP for this case. In a worker where u wont have H12 limit, just process the entire set once, no recursion, no segmenting. And use 'increment' op if it applies . – Robert Rowntree Jul 17 '18 at 15:54
  • @Robert Rowntree I tried to do exactly what you said. First code I wrote processed the entire 250k objects at once but I could not figure out how to make the code run on a worker dyno instead of the web dyno where I get the H12 errors. I posted a question about it some time ago. Could you maybe take a look and tell me what I did wrong? https://stackoverflow.com/questions/51118741/how-do-i-make-my-cloud-code-run-on-my-worker-dyno-instead-of-web-dyno – Murat Kaya Jul 17 '18 at 18:59
  • @Robert Rowntree can you also elaborate on what "increment atomic OP" is? – Murat Kaya Jul 17 '18 at 19:01
  • https://stackoverflow.com/questions/42892015/parse-server-atomic-increment-result – Robert Rowntree Jul 17 '18 at 19:05
  • https://devcenter.heroku.com/articles/background-jobs-queueing – Robert Rowntree Jul 17 '18 at 19:08
  • @Robert Rowntree thank you for those links. I have read the heroku link many times but I still don't see what I am doing wrong. – Murat Kaya Jul 17 '18 at 19:24

0 Answers0