ProGirlXOXO
ProGirlXOXO

Reputation: 2300

How to identify retried google cloud function?

We want to deploy a cloud function with retries:

gcloud function deploy somefunc --retry

We want to identify functions that are being retried:

if execution_is_a_retry = True:
    ...

How can we identify that a function is being retried?

Is there some parameter in the context / event data? Ideally we would also see the number of retries that have occurred.

I know that execution_id remains the same between the original run and a retry run but the only way to check that would be if the function writes the original id somewhere. In that case, it's possible that the function could fail before successfully writing it's execution_id somewhere and then consequent retries would not be identified as such.

Upvotes: 1

Views: 321

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

The retry invocation itself will not identify itself as a retry. You will have to determine this yourself, based in your own record of the event ID passed in the context parameter. If you see the same event ID more than once, you will know that your function was retried based on a prior failure. You won't know if the failure was because of your function, or something in Google's infrastructure.

You can get the event ID for a background function using context.eventID as described in the documentation.

If you do enable retries, you should look into making your function idempotent so that you don't unnecessarily or harmfully repeat work that it previously did. This will again require some record-keeping on your end to implement this correctly.

Upvotes: 5

Related Questions