go - 如何通过Google Cloud Tasks Go客户端库指定执行延迟?

标签 go scheduled-tasks google-cloud-tasks

我遵循this documentation创建任务,并阅读“您还可以微调任务的配置,例如安排将来应执行的时间”。
但是我仍然不清楚如何在我选择的延迟(time.Duration)中有效地安排执行时间,例如“请在20分钟内触发这些对象的清理”。
相关的LOC是CreateTaskRequest创建和Task创建:

        req := &taskspb.CreateTaskRequest{
                Parent: queuePath,
                Task: &taskspb.Task{
                        MessageType: &taskspb.Task_HttpRequest{
                                HttpRequest: &taskspb.HttpRequest{
                                        HttpMethod: taskspb.HttpMethod_POST,
                                        Url:        url,
                                },
                        },
                },
        }
        createdTask, err := client.CreateTask(ctx, req)
我应该考虑为此使用Cloud Scheduler吗?

最佳答案

您不需要Cloud Scheduler。
您要查找的是字段tasks.Task .ScheduleTime,其类型为* timestamppb.Timestamp
将您的time.Duration转换为将来的*timestamppb.Timestamp日期非常简单(此处忽略任何亚秒精度):

        var d time.Duration = 20 * time.Minute
            
        ts := &timestamppb.Timestamp{
            Seconds: time.Now().Add(d).Unix(),
        },

        req := &taskspb.CreateTaskRequest{
                Parent: queuePath,
                Task: &taskspb.Task{
                        MessageType: &taskspb.Task_HttpRequest{
                                HttpRequest: &taskspb.HttpRequest{
                                        HttpMethod: taskspb.HttpMethod_POST,
                                        Url:        url,
                                },
                        },
                        ScheduleTime: ts,
                },
        }
以上适用于API v2

关于go - 如何通过Google Cloud Tasks Go客户端库指定执行延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63942162/

相关文章:

go - 如何使用 go 1.5+ 创建静态链接的 golang 可执行文件

postgresql - Go 插入时偶尔出现 PostgreSQL "Duplicate key value violates unique constraint"错误

javascript - 需要 javascript 使按钮在一天中的特定时间出现/消失

java - Spring @Scheduled 任务运行两次

service-accounts - Google Cloud Tasks 无法对 Cloud Run 进行身份验证

Go lang Redis PubSub 在不同的 go 路由中用于发布和订阅

arrays - 切割排序 slice 的最佳方法

windows - 使用批处理文件检查服务器是否已启动?

typescript - 使用 Google Cloud Task 客户端部署 Firebase Functions 时收到区域错误

google-app-engine - Google Cloud 任务和 Google App Engine Python 3