tom
tom

Reputation: 2299

Retry count in deferred.defer in GAE

I'm using GAE's 'deffered' library (python), which automatically retries the task in the event an exception is raised.

Is there a way to know (within the task handler function) the number of times the task has been tried?

My end goal is to implement something like:

if num_tries >5:
  email_admins()
  raise deferred.PermanentTaskFailure

Initially I thought I could use 'TaskRetryOptions' to limit the number of tries, but I believe that doesnt provide a mechanism for my email_admins() call. Or does it?

[edit] of course I could read/write the number of tries to the DB or memcache, but I'd prefer to avoid that complexity. I'd prefer to get the details from the task / task queue if possible.

Upvotes: 3

Views: 1435

Answers (1)

lucemia
lucemia

Reputation: 6627

There are several headers will be set automatically with task https://developers.google.com/appengine/docs/python/taskqueue/overview-push

X-AppEngine-TaskRetryCount, the number of times this task has been retried; for the first attempt, this value is 0. This number includes attempts where the task failed due to a lack of available instances and never reached the execution phase.

X-AppEngine-TaskExecutionCount, the number of times this task has previously failed during the execution phase. This number does not include failures due to a lack of available instances.

EDIT 1

These values can access:

num_tries  = self.request.headers.get('X-AppEngine-TaskRetryCount')

EDIT 2

http://webapp-improved.appspot.com/api/webapp2.html#webapp2.get_request

for defered try:

request = webapp2.get_request()

Upvotes: 8

Related Questions