GAE Golang – 如何正确地将任务队列调度到后端?

huangapple go评论72阅读模式
英文:

GAE Golang - How to properly schedule a Task Queue to a Backend?

问题

在Google App Engine中使用Go语言调度任务队列到后端的方法并没有很多信息。在TQ的参考文档中,我们可以看到以下内容:

// 任务执行时要传递的额外HTTP头部。
// 要将任务调度到另一个应用版本或后端,请设置“Host”头部。
Header http.Header

但是并没有解释应该将“Host”设置为什么。在后端概述中,我们可以类似地看到:

> 私有后端可以被应用管理员、应用实例以及App Engine的API和服务(如任务队列任务和定时任务)访问,无需任何特殊配置。

但是同样没有给出解释。

我尝试将“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中,如何正确调度后端调用的方法是什么?

英文:

There is little information on how to schedule a Task Queue to a Backend in Google App Engine in Go. In TQ's Reference we can read:

// 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

But there is no explanation on what to really set the "Host" to. In Backends' Overview we can similarly read:

> Private backends can be accessed by application administrators, instances of the application, and by App Engine APIs and services (such as Task Queue tasks and Cron jobs) without any special configuration.

But again, no explanation is given.

I tried setting the "Host" value to the name of the backend, but the tasks are executed by the normal application.

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

What is the correct way to schedule a Backend call in GAE Go?

答案1

得分: 3

使用命名队列最容易定位后端。例如:

_, 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
英文:

It's easiest to target a backend by using a named queue. e.g.:

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

Your queue definition specifies the backend. e.g. for myQueueName, you might have a queue.yaml entry that looks like this:

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

答案2

得分: 2

使用appengine.BackendHostname函数来获取后端的主机名。这可以作为任务的Host头部使用。

英文:

Use the appengine.BackendHostname function to get the hostname for a backend. That should be usable as the Host header for a task.

huangapple
  • 本文由 发表于 2012年7月1日 09:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/11278901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定