首页 文章

GAE Golang - 如何正确地将任务队列安排到后端?

提问于
浏览
2

关于如何在Go中的Google App Engine中将任务队列安排到后端的信息很少 . 在TQ's Reference我们可以读到:

// Additional HTTP headers to pass at the task's execution time.
// To schedule the task to be run with an alternate app version
// or backend, set the "Host" header.
Header http.Header

但是没有解释什么真正设置“主机” . 在Backends的概述中,我们可以类似地阅读:

应用程序管理员,应用程序实例以及App Engine API和服务(例如任务队列任务和Cron作业)可以访问专用后端,而无需任何特殊配置 .

但同样,没有给出任何解释 .

我尝试将“Host”值设置为后端名称,但任务由普通应用程序执行 .

t := taskqueue.NewPOSTTask("/", map[string][]string{"key": {key}})
t.Header.Add("Host", "backend")
if _, err := taskqueue.Add(c, t, ""); err != nil {
    return
}

在GAE Go中安排后端呼叫的正确方法是什么?

2 回答

  • 2

    通过使用命名队列来定位后端是最容易的 . 例如 . :

    _, err = taskqueue.Add(c, &taskqueue.Task{
        Path:    "/myProcessorPath",
        Payload: myPayload,
    }, "myQueueName")
    

    您的队列定义指定后端 . 例如对于 myQueueName ,您可能有一个 queue.yaml 条目,如下所示:

    - name: myQueueName
      target: myBackendName
      rate: 400/s
      max_concurrent_requests: 64
      bucket_size: 25
      retry_parameters:
        task_age_limit: 7d
    
  • 3

    使用 appengine.BackendHostname 函数获取后端的主机名 . 这应该可以用作任务的主机头 .

相关问题