delay

package
v1.49.34 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package delay provides a task queue abstraction for background job execution. This is a standalone implementation that replaces the google.golang.org/appengine/delay package.

Index

Constants

View Source
const (
	// Path is the HTTP path for invocations (maintained for compatibility)
	Path = "/_/queue/delay"
)

Variables

View Source
var (
	// Funcs is the registry of all delayed functions
	Funcs = make(map[string]*Function)

	// DefaultRetryCount is the default number of retries for failed tasks
	DefaultRetryCount = 3

	// DefaultRetryDelay is the default delay between retries
	DefaultRetryDelay = time.Second * 5
)

Functions

func Drain added in v1.49.26

func Drain(ctx context.Context) error

Drain stops queued tasks from waiting any longer and waits for the ones already running to return, or for ctx to expire — whichever comes first. It returns ctx.Err() when the wait ran out of time, meaning tasks are still in flight and the datastore is not yet safe to close.

Call it during shutdown after the HTTP listener has drained, so nothing new is being queued, and before closing the database. Both callers do exactly that: App.Shutdown and the test context's Close.

Draining is not permanent. Once the wait is over the queue re-arms, so a process that drains for a checkpoint keeps working and a test binary running several contexts in sequence gets a clean queue for each.

func Later

func Later(ctx context.Context, delay time.Duration, fn func(context.Context) error) error

Later executes a function after a delay. This is a simpler API for one-off delayed tasks.

func Now

func Now(ctx context.Context, fn func(context.Context) error) error

Now executes a function immediately in a background goroutine.

Types

type Function

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

Function wraps a delayed function with queue configuration

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 FuncByKey

func FuncByKey(key string) *Function

FuncByKey retrieves a registered function by its key.

func (*Function) After added in v1.49.26

func (f *Function) After(d time.Duration) *Function

After returns a copy of this Function whose calls wait d before running.

It exists so a caller that wants to re-queue work with a backoff can say so through the queue instead of writing its own `go func` around a Sleep. A hand-rolled goroutine is untracked: it registers its task only after waking, which is too late for a Drain that has already counted the parent out.

func (*Function) Call

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

Call invokes a delayed function asynchronously using goroutines. The function is executed in a background goroutine after any configured delay.

func (*Function) Queue

func (f *Function) Queue(queue string) *Function

Queue returns a copy of this Function with the specified queue.

func (*Function) Task

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

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

type RequestHeaders

type RequestHeaders struct {
	TaskName       string
	TaskRetryCount int64
	QueueName      string
}

RequestHeaders contains metadata from the task queue request. This replaces taskqueue.RequestHeaders from appengine.

func ParseRequestHeaders

func ParseRequestHeaders(h http.Header) *RequestHeaders

ParseRequestHeaders extracts task queue headers from an HTTP request. This is a compatibility function for HTTP-based task invocation.

type Task

type Task struct {
	ID      string
	Path    string
	Payload []byte
	Options TaskOptions
}

Task represents a delayed task

type TaskOptions

type TaskOptions struct {
	Queue      string
	Name       string
	Delay      time.Duration
	RetryCount int
	RetryDelay time.Duration
}

TaskOptions configures task execution

Jump to

Keyboard shortcuts

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