为使用 GAE 延迟库排队的任务指定重试限制

Specifying retry limit for tasks queued using GAE deferred library

我们正在使用 GAE deferred library 卸载某些耗时的任务,并且想知道我们如何为这些卸载的任务设置重试限制。我们 运行 遇到某些任务永远重试的问题,因为由于某些不可恢复的异常,任务永远不会成功。

根据documentation

queue:
- name: fooqueue
  rate: 1/s
  retry_parameters:
    task_retry_limit: 7
    task_age_limit: 2d
- name: barqueue
  rate: 1/s
  retry_parameters:
    min_backoff_seconds: 10
    max_backoff_seconds: 200
    max_doublings: 0
- name: bazqueue
  rate: 1/s
  retry_parameters:
    min_backoff_seconds: 10
    max_backoff_seconds: 200
    max_doublings: 3

检查任务中的 X-Appengine-TaskretrycountX-Appengine-Taskexecutioncount http header 值。

如果您不想重试任务,可以引发 deferred.PermanentTaskFailure 异常。此异常将仅被记录,任务不会再次 运行。

访问http的不同方式headers:

对于任务队列触发的 http 处理程序: num_tries = self.request.headers.get('X-AppEngine-TaskRetryCount')

延迟库触发的函数: num_tries = webapp2.get_request().headers.get('X-AppEngine-TaskRetryCount')

根据 the documentation deferred.defer API 的 _retry_options 可用于将重试选项传递给关联的 Task() 实例:

_countdown, _eta, _headers, _name, _target, _transactional, _url, _retry_options, _queue: Passed through to the task queue - see the task queue documentation for details.

来自the Task() doc

...

你可以使用 TaskRetryOptions()'s task_retry_limit 属性:

task_retry_limit

The maximum number of retry attempts for a failed task.

In push queues, the counter is incremented each time App Engine tries the task, up to the specified task_retry_limit. If specified with task_age_limit, App Engine retries the task until both limits are reached.

In pull queues, the counter is incremented each time the task is leased, up to the specified task_retry_limit. Tasks are deleted automatically once they have been leased the number of times specified in the limit.

注意:答案仅基于文档,我没有实际实现,YMMV。