Execution after timeout
Stay organized with collections
Save and categorize content based on your preferences.
Sample that demonstrates code that should (but won't) execute after a GCF call times out.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates code samples in Go, Java, Node.js, and Python that are designed to execute for two minutes, showcasing how Cloud Functions might time out before completion.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code examples highlight the potential for a function's execution to be interrupted by a timeout if the set duration is less than two minutes, as each sample includes a two-minute delay.\u003c/p\u003e\n"],["\u003cp\u003eThe samples use \u003ccode\u003etime.Sleep\u003c/code\u003e in Go, \u003ccode\u003eTimeUnit.MINUTES.sleep\u003c/code\u003e in Java, \u003ccode\u003esetTimeout\u003c/code\u003e in Node.js, and \u003ccode\u003etime.sleep\u003c/code\u003e in Python, to simulate a long-running process that could be cut short by a timeout.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for Cloud Run functions is achieved through setting up Application Default Credentials, with a link provided for further instructions on local development environment authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe content directs users to the Google Cloud sample browser for further exploration of code samples across various Google Cloud products.\u003c/p\u003e\n"]]],[],null,["# Execution after timeout\n\nSample that demonstrates code that should (but won't) execute after a GCF call times out.\n\nCode sample\n-----------\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package tips contains tips for writing Cloud Functions in Go.\n package tips\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"time\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.HTTP(\"Timeout\", Timeout)\n }\n\n // Timeout sleeps for 2 minutes and may time out before finishing.\n func Timeout(w http.ResponseWriter, r *http.Request) {\n \tlog.Println(\"Function execution started...\")\n \ttime.Sleep(2 * time.Minute)\n \tlog.Println(\"Function completed!\")\n \tfmt.Fprintln(w, \"Function completed!\")\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package functions;\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.BufferedWriter;\n import java.io.IOException;\n import java.util.concurrent.TimeUnit;\n import java.util.logging.Logger;\n\n public class AfterTimeout implements HttpFunction {\n private static final Logger logger = Logger.getLogger(AfterTimeout.class.getName());\n\n // Simple function to return \"Hello World\"\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException, InterruptedException {\n logger.info(\"Function running...\");\n TimeUnit.MINUTES.sleep(2);\n\n // May not execute if function's timeout is \u003c2 minutes\n logger.info(\"Function completed!\");\n BufferedWriter writer = response.getWriter();\n writer.write(\"Function completed!\");\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const functions = require('@google-cloud/functions-framework');\n\n /**\n * HTTP Cloud Function that may not completely\n * execute due to function execution timeout\n *\n * @param {Object} req Cloud Function request context.\n * @param {Object} res Cloud Function response context.\n */\n functions.http('afterTimeout', (req, res) =\u003e {\n setTimeout(() =\u003e {\n // May not execute if function's timeout is \u003c2 minutes\n console.log('Function running...');\n res.end();\n }, 120000); // 2 minute delay\n });\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import time\n\n import functions_framework\n\n\n @functions_framework.http\n def timeout(request):\n print(\"Function running...\")\n time.sleep(120)\n\n # May not execute if function's timeout is \u003c2 minutes\n print(\"Function completed!\")\n return \"Function completed!\"\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]