delay

package
v1.41.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 10 Imported by: 0

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

func RunFunc

func RunFunc(c context.Context, w http.ResponseWriter, req *http.Request)

RunFunc handles HTTP requests to execute delayed functions. This is maintained for backward compatibility with HTTP-based task queues. In the new implementation, tasks are executed directly via goroutines, but this handler can still be used if you want to dispatch tasks via HTTP.

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) 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) Once

func (f *Function) Once(ctx context.Context, name string, delay time.Duration, args ...interface{}) error

Once adds a task only once by using a unique name. This prevents duplicate task execution.

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 GetRequestHeaders

func GetRequestHeaders(ctx context.Context) *RequestHeaders

GetRequestHeaders retrieves the task queue headers from the context. Returns nil if called outside of a delay function execution.

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