gpt4 book ai didi

google-cloud-platform - GCP 云任务 : shorten period for creating a previously created named task

转载 作者:行者123 更新时间:2023-12-05 07:01:58 26 4
gpt4 key购买 nike

我们正在开发一个基于 GCP Cloud Task 的队列进程,该进程会在特定 Firestore 文档写入触发器触发时发送状态电子邮件。我们使用 Cloud Tasks 的原因是可以在发送电子邮件之前创建延迟(在未来使用 scheduledTime 属性 2 分钟),并控制重复数据删除(通过使用格式为以下格式的任务名称:[firestore-collection-name ]-[doc-id]) 因为在创建文档时可以多次触发 Firestore 文档上的“写入”触发器,然后由后端云功能快速更新。

一旦达到任务的延迟期,云任务就会运行,并发送包含更新的 Firestore 文档信息的电子邮件。之后任务从队列中删除,一切正常。

除了:

如果用户更新 Firestore 文档(比如 20 或 30 分钟后),我们想重新发送状态电子邮件,但无法使用相同的任务名称创建任务。我们收到以下错误:

409 The task cannot be created because a task with this name existed too recently. For more information about task de-duplication see https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks/create#body.request_body.FIELDS.task.

这是出乎意料的,因为此时队列是空的,因为最后一个任务已成功完成。错误消息中引用的文档说:

If the task's queue was created using Cloud Tasks, then another taskwith the same name can't be created for ~1hour after the original taskwas deleted or executed.

问题:有没有什么方法可以通过减少时间量,甚至完全取消限制来绕过这个限制?

最佳答案

简短的回答是否定的。正如您已经指出的那样,文档对这种行为非常清楚,您应该等待 1 小时才能创建一个与之前创建的任务同名的任务。 API 或客户端库不允许减少此时间。

话虽如此,我还是建议不要使用相同的任务 ID,而是为任务使用不同的 ID,并在请求正文中添加一个标识符。例如,使用 Python:

from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2
import datetime

def create_task(project, queue, location, payload=None, in_seconds=None):
client = tasks_v2.CloudTasksClient()

parent = client.queue_path(project, location, queue)

task = {
'app_engine_http_request': {
'http_method': 'POST',
'relative_uri': '/task/'+queue
}
}
if payload is not None:
converted_payload = payload.encode()

task['app_engine_http_request']['body'] = converted_payload

if in_seconds is not None:
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)

timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)

task['schedule_time'] = timestamp

response = client.create_task(parent, task)

print('Created task {}'.format(response.name))
print(response)

#You can change DOCUMENT_ID with USER_ID or something to identify the task
create_task(PROJECT_ID, QUEUE, REGION, DOCUMENT_ID)

关于google-cloud-platform - GCP 云任务 : shorten period for creating a previously created named task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63694524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com