The delay package

import ""


Package delay provides a way to execute code outside the scope of a user request by using the taskqueue API.

To declare a function that may be executed later, call Func in a top-level assignment context, passing it an arbitrary string key and a function whose first argument is of type context.Context.

var laterFunc = delay.Func("key", myFunc)

It is also possible to use a function literal.

var laterFunc = delay.Func("key", func(c context.Context, x string) {
    // ...

To call a function, invoke its Call method.

laterFunc.Call(c, "something")

A function may be called any number of times. If the function has any return arguments, and the last one is of type error, the function may return a non-nil error to signal to the queue that the function should be retried.

The arguments to functions may be of any type that is encodable by the gob package. If an argument is of interface type, it is the client's responsibility to register with the gob package whatever concrete type may be passed for that argument; see for details.

Any errors during initialization or execution of a function will be logged to the application logs. Error logs that occur during initialization will be associated with the request that invoked the Call method.

The state of a function invocation that has not yet successfully executed is preserved by combining the file name in which it is declared with the string key that was passed to the Func function. Updating an app with pending function invocations is safe as long as the relevant functions have the (filename, key) combination preserved.

The delay package uses the Task Queue API to create tasks that call the reserved application path "/_ah/queue/go/delay". This path must not be marked as "login: required" in app.yaml; it must be marked as "login: admin" or have no access restriction.


type Function
    func Func(key string, i interface{}) *Function
    func (f *Function) Call(c context.Context, args ...interface{}) error
    func (f *Function) Task(args ...interface{}) (*taskqueue.Task, error)

type Function

type Function struct {
    // contains filtered or unexported fields

Function represents a function that may have a delayed invocation.

func Func

func Func(key string, i interface{}) *Function

Func declares a new Function. The second argument must be a function with a first argument of type context.Context. This function must be called at program initialization time. That means it must be called in a global variable declaration or from an init function. This restriction is necessary because the instance that delays a function call may not be the one that executes it. Only the code executed at program initialization time is guaranteed to have been run by an instance before it receives a request.

func (*Function) Call

func (f *Function) Call(c context.Context, args ...interface{}) error

Call invokes a delayed function.

err := f.Call(c, ...)

is equivalent to

t, _ := f.Task(...)
_, err := taskqueue.Add(c, t, "")

func (*Function) Task

func (f *Function) Task(args ...interface{}) (*taskqueue.Task, error)

Task creates a Task that will invoke the function. Its parameters may be tweaked before adding it to a queue. Users should not modify the Path or Payload fields of the returned Task.