taskqueue 软件包

import "google.golang.org/appengine/taskqueue"

简介

taskqueue 软件包为 App Engine 任务队列服务提供了客户端。 使用此服务,应用可以执行用户请求以外的工作。

可以手动构建任务;或者,可以使用 NewPOSTTask,因为最常见的任务队列操作是添加单个 POST 任务,而 NewPOSTTask 可让您轻松添加 POST 任务。

t := taskqueue.NewPOSTTask("/worker", url.Values{
    "key": {key},
})
taskqueue.Add(c, t, "") // add t to the default queue

索引

Variables
func Add(c context.Context, task *Task, queueName string) (*Task, error)
func AddMulti(c context.Context, tasks []*Task, queueName string) ([]*Task, error)
func Delete(c context.Context, task *Task, queueName string) error
func DeleteMulti(c context.Context, tasks []*Task, queueName string) error
func Lease(c context.Context, maxTasks int, queueName string, leaseTime int) ([]*Task, error)
func LeaseByTag(c context.Context, maxTasks int, queueName string, leaseTime int, tag string) ([]*Task, error)
func ModifyLease(c context.Context, task *Task, queueName string, leaseTime int) error
func Purge(c context.Context, queueName string) error
func QueueStats(c context.Context, queueNames []string) ([]QueueStatistics, error)
type QueueStatistics
type RetryOptions
type Task
    func NewPOSTTask(path string, params url.Values) *Task

变量

var (
    // ErrTaskAlreadyAdded is the error returned by Add and AddMulti when a task has already been added with a particular name.
    ErrTaskAlreadyAdded = errors.New("taskqueue: task has already been added")
)

func Add

func Add(c context.Context, task *Task, queueName string) (*Task, error)

Add 会将任务添加到命名的队列中。 如果队列名为空,则表示将使用默认队列。 Add 会返回一个填充了默认值的等效任务,包括将任务的“名称”字段设置为所选名称(如果初始名称为空)。

func AddMulti

func AddMulti(c context.Context, tasks []*Task, queueName string) ([]*Task, error)

AddMulti 会将多个任务添加到命名的队列中。 如果队列名为空,则表示将使用默认队列。 AddMulti 会返回一部分填充了默认值的等效任务,包括将每个任务的“名称”字段设置为所选名称(如果初始名称为空)。 如果给定任务格式错误或无法添加,则会返回 appengine.MultiError。

func Delete

func Delete(c context.Context, task *Task, queueName string) error

Delete 会将任务从命名的队列中删除。

func DeleteMulti

func DeleteMulti(c context.Context, tasks []*Task, queueName string) error

DeleteMulti 会将多个任务从命名的队列中删除。 如果无法删除给定任务,则会返回 appengine.MultiError。

func Lease

func Lease(c context.Context, maxTasks int, queueName string, leaseTime int) ([]*Task, error)

Lease 会从队列租用任务。leaseTime 以秒为单位。提取的任务数量最多为 maxTasks。

func LeaseByTag

func LeaseByTag(c context.Context, maxTasks int, queueName string, leaseTime int, tag string) ([]*Task, error)

LeaseByTag 会从队列租用任务(按标签分组)。 如果标签为空,则返回的任务按预计到达时间最早的任务的标签进行分组。leaseTime 以秒为单位。 提取的任务数量最多为 maxTasks。

func ModifyLease

func ModifyLease(c context.Context, task *Task, queueName string, leaseTime int) error

ModifyLease 会修改任务的租约。 用于请求更多处理时间,或用于放弃处理。leaseTime 以秒为单位,不得为负数。

func Purge

func Purge(c context.Context, queueName string) error

Purge 会将所有任务从队列中移除。

func QueueStats

func QueueStats(c context.Context, queueNames []string) ([]QueueStatistics, error)

QueueStats 会检索有关队列的统计信息。

type QueueStatistics

type QueueStatistics struct {
    Tasks     int       // may be an approximation
    OldestETA time.Time // zero if there are no pending tasks

    Executed1Minute int     // tasks executed in the last minute
    InFlight        int     // tasks executing now
    EnforcedRate    float64 // requests per second
}

QueueStatistics 表示有关单个任务队列的统计信息。

type RetryOptions

type RetryOptions struct {
    // Number of tries/leases after which the task fails permanently and is deleted.
    // If AgeLimit is also set, both limits must be exceeded for the task to fail permanently.
    RetryLimit int32

    // Maximum time allowed since the task's first try before the task fails permanently and is deleted (only for push tasks).
    // If RetryLimit is also set, both limits must be exceeded for the task to fail permanently.
    AgeLimit time.Duration

    // Minimum time between successive tries (only for push tasks).
    MinBackoff time.Duration

    // Maximum time between successive tries (only for push tasks).
    MaxBackoff time.Duration

    // Maximum number of times to double the interval between successive tries before the intervals increase linearly (only for push tasks).
    MaxDoublings int32

    // If MaxDoublings is zero, set ApplyZeroMaxDoublings to true to override the default non-zero value.
    // Otherwise a zero MaxDoublings is ignored and the default is used.
    ApplyZeroMaxDoublings bool
}

RetryOptions 可让您控制是否重试任务并控制两次尝试之间的暂停间隔。

type Task

type Task struct {
    // Path is the worker URL for the task.
    // If unset, it will default to /_ah/queue/<queue_name>.
    Path string

    // Payload is the data for the task.
    // This will be delivered as the HTTP request body.
    // It is only used when Method is POST, PUT or PULL.
    // url.Values' Encode method may be used to generate this for POST requests.
    Payload []byte

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

    // Method is the HTTP method for the task ("GET", "POST", etc.),
    // or "PULL" if this is task is destined for a pull-based queue.
    // If empty, this defaults to "POST".
    Method string

    // A name for the task.
    // If empty, a name will be chosen.
    Name string

    // Delay specifies the duration the task queue service must wait
    // before executing the task.
    // Either Delay or ETA may be set, but not both.
    Delay time.Duration

    // ETA specifies the earliest time a task may be executed (push queues)
    // or leased (pull queues).
    // Either Delay or ETA may be set, but not both.
    ETA time.Time

    // The number of times the task has been dispatched or leased.
    RetryCount int32

    // Tag for the task. Only used when Method is PULL.
    Tag string

    // Retry options for this task. May be nil.
    RetryOptions *RetryOptions
}

Task 表示要执行的任务。

func NewPOSTTask

func NewPOSTTask(path string, params url.Values) *Task

NewPOSTTask 会创建一个将使用给定表单数据对路径执行 POST 操作的任务。