'Inspect Google Cloud Task Queue retry parameters from Task
I have created a Cloud Task that has a retry policy with max attempts set to (for example 4).
However, we want the Task to fallback to a default solution if the Task fails for the last time.
That is, if the Task fails on the first time we just log the error and return 500. And again for the second time. And the third. Etc... but if the current TaskRetryCount is the last attempt for that queue (i.e. 4 in this example) then we will execute some fallback behaviour before returning 500 for the last time.
I can see from the Cloud Tasks HTTP Handler docs that I can get the X-CloudTasks-TaskRetryCount from the header of the request. But it doesn't seem to have a way to get the max-attempts for the queue.
Is that something that I can get hold of?
Or am I approaching this wrongly? Is there a better way of doing what I'm trying to do.
Solution 1:[1]
Assuming that the tasks are created with Cloud Tasks the queue-level retry settings apply to all tasks in the queue that were created using Cloud Tasks (as they cannot be set on individual tasks). Calling the projects.locations.queues.get method from the REST API should return the RetryConfig settings from the Queue that include the maxAttempts field that you require.
For tasks created with the App Engine SDK you should refer to this docs (although I believe that approach is deprecated and not useful for your use case).
Solution 2:[2]
You can retrieve the queue max attempts value using getQueue call and inspecting queue.retryConfig.maxAttempts, and use the header X-CloudTasks-TaskRetryCount to check the current retry, if maxAttempt != -1 && maxAttempt == (retry+1) your in the last retry.
Note that X-CloudTasks-TaskRetryCount counting only tasks that return 5xx status code, and also tasks that did not get to the execution phase (to your code).
If you want to count only failed attempts happend in your exeuction phase, you should use the header X-CloudTasks-TaskExecutionCount
and make sure your code return 4xx status code in case of an error.
As per the docs:
X-CloudTasks-TaskExecutionCount The total number of times that the task has received a response from the handler. Since Cloud Tasks deletes the task once a successful response has been received, all previous handler responses were failures. This number does not include failures due to 5XX error codes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Ocando |
| Solution 2 |
