0

I have a Node JS App Engine app that uses Request Promises Native to fetch external URLs. App Engine scripts launched from Cloud Tasks are supposed to have up to a 24 hour deadline, but all my tasks are being killed after 60 seconds. I know other frameworks have to use a built-in URLFetch library, and this gives them an automatic 60 second task deadline, but the docs don't state anything about Node.JS bases app engine tasks. This task fetches json from an external URL, then processes the results and inserts them into firebase, which is typically what is happening when the task is killed with an HTTP 504 Deadline Exceeded error

app.post('/tasks/import', async (req, res) => {
  const paramsObj = JSON.parse(Buffer.from(req.body, 'base64').toString('utf-8'));

  const page = paramsObj.page * 1;
  const storeID = paramsObj.storeid;
  const pageSize = 50;
  const config: StoreConfig = await getConfigById(storeID);

  const options = {
    uri: EXTERNAL_URL,
    json: true,
    resolveWithFullResponse: false,
    qs: {
      limit: pageSize,
      page: page,
    },
  };

  try {
    const results = await rp.get(options);
    if (results.products.length === 0) {
      return res.status(200).end();
    }

    const prodLooper = idx => {
      const product = results.products[idx];
      product.store_id = storeID;
      product.body_html = cleanProductBody(product.body_html);
      getUnusedUPC(storeID)
        .then(upcID => {
          product.upc = upcID;

          fsdb
            .collection('products')
            .add(product)
            .then(() => {
              idx++;
              if (idx < results.products.length) {
                prodLooper(idx);
              } else {

                return res.send('OK').end();
              }
            });
        })
        .catch(err => {
          console.log(err.code, ':', page, ':', idx);
          if (err.code === 10) {
            setTimeout(() => {
              prodLooper(idx);
            }, 10);

          }
        });
    };
    prodLooper(0);
  } catch (error) {
    console.log('caught error:');
    console.log(error.statusCode);
    console.log(error.message);
    return res.status(500).end();
  }
});
regretoverflow
  • 2,093
  • 1
  • 23
  • 45
  • Were you able to fix this at all? I am getting a similar error when I try to create a new queue from my cloud function. – nasaa Mar 07 '19 at 16:05
  • No, I ended up having to re-write it using pub/sub so that each task runs independently from the previous – regretoverflow Mar 19 '19 at 16:24

1 Answers1

0

As per the documentation:

google.appengine.runtime.DeadlineExceededError: raised if the overall request times out, typically after 60 seconds, or 10 minutes for task queue requests.

Same documentation also provides some common errors that can cause this, and suggestions on how to avoid them.

Maxim
  • 4,075
  • 1
  • 14
  • 23