MartinSahlen/cloud-functions-python

Cloud Function taking more than default of 60 seconds, it auto terminated.

santhoshdc1590 opened this issue · 2 comments

Cloud-function I deployed works fine. In some cases the function might take longer to execute than a minute. Cloud-Function has be set to terminate the function exactly after a minute.

I found a google documentation here that allows us to run a cloud-function upto 9 minutes.

This is the normal code of node.js to check the function is executed within a minute

/*** HTTP Cloud Function that may not completely
 * execute due to early HTTP response
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.afterResponse = (req, res) => {
  res.end();

  // This statement may not execute
  console.log('Function complete!');
}; 

This is the snippet below for it to run for more than a minute, this example it is set to 2 minutes.

 /**
 * HTTP Cloud Function that may not completely
 * execute due to function execution timeout
 *
 * @param {Object} req Cloud Function request context.
 * @param {Object} res Cloud Function response context.
 */
exports.afterTimeout = (req, res) => {
  setTimeout(() => {
    // May not execute if function's timeout is <2 minutes
    console.log('Function running...');
    res.end();
  }, 120000); // 2 minute delay
};

Which code inside index.js should I alter to extend the time?

There is no need to alter code to achieve this, you set the timeout at deploy time :) https://cloud.google.com/sdk/gcloud/reference/beta/functions/deploy