【发布时间】:2020-01-26 18:36:00
【问题描述】:
我正在尝试创建一个在 HTTP 任务失败时从不重试的 Cloud Tasks 队列。
根据documentation,maxAttempts 应该是我要找的:
每个任务的尝试次数。
Cloud Tasks 将尝试任务 maxAttempts 次(也就是说,如果 第一次尝试失败,然后将有 maxAttempts - 1 次重试)。必须 是 >= -1。
因此,如果 maxAttempts 为 1,则应该有 0 次重试。
但是,例如,如果我运行
gcloud tasks queues create test-queue --max-attempts=1 --log-sampling-ratio=1.0
然后使用以下 Python 代码创建 HTTP 任务:
from google.cloud import tasks_v2beta3
from google.protobuf import timestamp_pb2
client = tasks_v2beta3.CloudTasksClient()
project = 'project_id' # replace by real project ID
queue = 'test-queue'
location = 'us-central1'
url = 'https://example.com/task_handler' # replace by some endpoint that return 5xx status code
parent = client.queue_path(project, location, queue)
task = {
'http_request': { # Specify the type of request.
'http_method': 'POST',
'url': url # The full url path that the task will be sent to.
}
}
response = client.create_task(parent, task)
print('Created task {}'.format(response.name))
在队列的 Stackdriver 日志中(我可以看到,因为我在创建队列时使用了 --log-sampling-ratio=1.0),任务显然重试了一次:有一次调度尝试,然后是状态为 UNAVAILABLE 的调度响应,然后由另一个调度尝试,最后是最后一个调度响应(也表示不可用)。
有没有办法重试0次?
注意
关于 maxAttempts,文档还说:
该字段与queue.yaml/xml中的task_retry_limit含义相同。
但是,当我转到description for task_retry_limit 时,它说:
重试次数。例如,如果指定了 0 并且任务 失败,任务根本不会重试。如果指定 1 并且任务 失败,任务重试一次。如果未指定此参数,则 任务无限期重试。如果 task_retry_limit 指定为 task_age_limit,重试任务,直到达到两个限制。
这似乎与maxAttempts的描述不一致,因为它表明如果参数为1,任务将重试一次。
我尝试将 maxAttempts 设置为 0,但这似乎使它假定默认值为 100。
提前谢谢你。
【问题讨论】:
-
我添加了一个错误来清除 max_attempts 和 task_retry_limit 之间的区别。不过我很好奇,你知道你的处理程序返回什么响应代码吗?
-
@AveriKitsch - 谢谢。我已经用一个总是返回响应代码 500 的虚拟处理程序进行了测试。但我首先注意到它发生在一个“真实”处理程序上,该处理程序有一个临时错误并在第一次调度中返回 500,然后该任务随后重试了 1 次并成功(即返回 200)。
-
我很抱歉响应缓慢。这是一个错误。目前我无法估计修复程序何时发布。
-
@AveriKitsch 没问题,谢谢您的回复。
标签: google-cloud-platform google-cloud-tasks