Mor*_*ess 3 google-app-engine go google-cloud-platform
我无法让 appengine 任务队列接受我抛出的任何上下文:
import (
"context"
"google.golang.org/appengine"
"google.golang.org/appengine/taskqueue"
)
/* snip */
ctx:= context.Background()
task := taskqueue.NewPOSTTask("/b/mytask", params)
_, err = taskqueue.Add(ctx, task, "")
if err != nil {
return fmt.Errorf("adding background task with path %s: %v", task.Path, err)
}
Run Code Online (Sandbox Code Playgroud)
如go111 迁移文档所述,我在 main.go 主函数中调用 appengine.Main() (但在go112 迁移文档中缺少这一行,因此我不确定是否需要)。
我试过了:
context.Background()
request.Context()
appengine.NewContext(r)
appengine.BackgroundContext()
context.TODO()
Run Code Online (Sandbox Code Playgroud)
都导致错误:
不是 App Engine 上下文
除了appengine.BackgroundContext()获得:
服务桥 HTTP 失败:发布 http://appengine.googleapis.internal:10001/rpc_http : dial tcp 169.254.169.253:10001: i/o timeout
在将 GAE 标准项目从 go19 迁移到 go112 以使用 go 模块时,我遇到了同样的问题。此外,我收到了很多“502 错误网关”消息。
用appengine.Main()替换 main() 中的 http.ListenAndServe()修复了上下文问题。移动到go111而不是 112 解决了另一个问题。文档和示例对此不是很清楚。