George Kemp
George Kemp

Reputation: 631

First Invocation of Google Cloud Function always times out

I have a google cloud function that's seems to timeout after being inactive for a certain amount of time or if I re-deploy it. Subsequent calls to the end point work just fine, it's just the initial invocation which doesn't work. The following is an over simplified version of what my cloud function is. I basically use an express app as a handler. Perhaps the issue is with the express app not running the first time around, but running on subsequent invocations?

const express = require('express');
const app = express();

const cors = require('cors');

app.use(cors())

app.get('/health', (req, res) => {
  res.send('OK');
});

module.exports = app;

Currently have out set to 60s, and a route like the health route shouldn't take that long.

Some interesting log entries

"Function execution took 60004 ms, finished with status: 'timeout'" 
textPayload: "Error: Retry total timeout exceeded before any response was received
    at repeat (/srv/functions/node_modules/google-gax/build/src/normalCalls/retries.js:80:31)
    at Timeout.setTimeout [as _onTimeout] (/srv/functions/node_modules/google-gax/build/src/normalCalls/retries.js:113:25)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)" 

Upvotes: 2

Views: 1456

Answers (2)

Andrei Tigau
Andrei Tigau

Reputation: 2058

Cloud Function execution time is limited by the timeout duration, which you can specify at function deployment time. By default, a function times out after 1 minute. As it is stated in the official documentation:

When function execution exceeds the timeout, an error status is immediately returned to the caller. CPU resources used by the timed-out function instance are throttled and request processing may be immediately paused. Paused work may or may not proceed on subsequent requests, which can cause unexpected side effects.

Note that this period can be extended up to 9 minutes. In order to set the functions timeout limit you can use this gcloud command:

gcloud functions deploy FUNCTION_NAME --timeout=TIMEOUT FLAGS...

More details about your options could be found over here. But, maybe if your code takes a long time to execute, you may also consider using another serverless option, like Cloud Run.

Upvotes: 1

Kolban
Kolban

Reputation: 15266

A Google Cloud Function can be thought of as the event handler for an incoming event request. A cloud function can be triggered from a REST request, pub/sub or cloud storage. For a REST request, consider the function that you supply as the one and only "handler" that the function offers.

The code that you supply (assuming Node.JS) is a function that is passedin an express request object and response object. In the body of the function, you are responsible for handling the request.

Specifically, your Cloud Function should not set up express or attempt to otherwise modify the environment. The Cloud Function provides the environment to be called externally and you provide the logic to be called. Everything else (scaling etc) is handled by Google.

Upvotes: 0

Related Questions