Ocean Overflow
Ocean Overflow

Reputation: 561

How to exit running event-triggered Cloud Function from within itself?

I want to terminate and exit running cloud function. Function was triggered by Firestore event. What are some ways to do this?

Upvotes: 1

Views: 1152

Answers (1)

Osvaldo
Osvaldo

Reputation: 509

There are some reasons why you want a Cloud Function to terminate itself, for example, to avoid an infinite loop or infinite retries.

To avoid infinite retry loops, set an end condition. You can do this by including a well-defined end condition, before the function begins processing.

A simple yet effective approach is to discard events with timestamps older than a certain time. This helps to avoid excessive executions when failures are either persistent or longer-lived than expected.

Events are delivered at least once, but a single event may result in multiple function invocations. Avoid depending on exactly-once mechanics and write idempotent functions.

Note that updating the function-triggering Firestore document may create subsequent update events, which may cascade into an infinite loop within your function. To solve this problem, use trigger types that ignore updates (such as document.create), or configure your function to only write to Firestore if the underlying value has changed.

Also, note the limitations for Firestore triggers for Cloud Functions.

You might also want to check this example about Cloud Function Termination. Do not manually exit a Function; it can cause unexpected behavior.

Upvotes: 1

Related Questions