delay

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2021 License: Apache-2.0 Imports: 18 Imported by: 3

Documentation

Overview

Package delay provides a way to execute code outside of the scope of a user request by using the Task Queue API. To use a deferred function, you must register the function to be deferred as a top-level var. For example,

```
var laterFunc = delay.MustRegister("key", myFunc)
func myFunc(ctx context.Context, a, b string) {...}
```

You can also inline with a function literal:

```
var laterFunc = delay.MustRegister("key", func(ctx context.Context, a, b string) {...})
```

In the above example, "key" is a logical name for the function. The key needs to be globally unique across your entire application. To invoke the function in a deferred fashion, call the top-level item:

```
laterFunc(ctx, "aaa", "bbb")
```

This will queue a task and return quickly; the function will be actually run in a new request. The delay package uses the Task Queue API to create tasks that call the reserved application path "/_ah/queue/go/delay". This path may only be marked as "login: admin" or have no access restriction; it will fail if marked as "login: required".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequestHeaders

func RequestHeaders(c context.Context) (*taskqueue.RequestHeaders, error)

Request returns the special task-queue HTTP request headers for the current task queue handler. Returns an error if called from outside a delay.Func.

Types

type Function

type Function struct {
	// contains filtered or unexported fields
}

Function represents a function that may have a delayed invocation.

func Func deprecated

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

Func declares a new function that can be called in a deferred fashion. The second argument i must be a function with the first argument of type context.Context. To make the key globally unique, the SDK code will combine "key" with the filename of the file in which myFunc is defined (e.g., /some/path/myfile.go). This is convenient, but can lead to failed deferred tasks if you refactor your code, or change from GOPATH to go.mod, and then re-deploy with in-flight deferred tasks.

This function Func must be called in a global scope to properly register the function with the framework.

Deprecated: Use MustRegister instead.

func MustRegister

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

MustRegister declares a new function that can be called in a deferred fashion. The second argument i must be a function with the first argument of type context.Context. MustRegister requires the key to be globally unique.

This function MustRegister must be called in a global scope to properly register the function with the framework. See the package notes above for more details.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL