3

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.

tom
  • 2,189
  • 2
  • 15
  • 27
  • I would suggest using the accepted solution here - http://stackoverflow.com/questions/36604616/specifying-retry-limit-for-tasks-queued-using-gae-deferred-library/36621588#36621588 – keerthy Apr 14 '16 at 16:25

1 Answers1

8

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()
Community
  • 1
  • 1
lucemia
  • 6,349
  • 5
  • 42
  • 75
  • 1
    Yes, but is there an easy way to get at these when using `deferred`? – Jesse Rusak Oct 25 '13 at 01:41
  • 1
    is self.request valid in the context of deferred? – tom Oct 25 '13 at 01:42
  • 1
    but a task handler is not executing in the context of a webapp(2) request. I'll give it a try anyway.. – tom Oct 25 '13 at 01:48
  • 3
    I tested it, and webapp2.get_request().headers.get('X-AppEngine-TaskRetryCount') does indeed work - thanks! – tom Oct 25 '13 at 01:55
  • I would suggest using the accepted solution here - http://stackoverflow.com/questions/36604616/specifying-retry-limit-for-tasks-queued-using-gae-deferred-library/36621588#36621588 – keerthy Apr 14 '16 at 16:25