tasks

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrHandlerMissing = errors.New("task handler is required")

ErrHandlerMissing indicates a job handler was not provided.

View Source
var ErrRunnerClosed = errors.New("task runner is closed")

ErrRunnerClosed indicates the runner is shutting down.

Functions

func DefaultRetryDecider

func DefaultRetryDecider(err error) bool

DefaultRetryDecider retries unless the error is from context cancellation.

Types

type BackoffFunc

type BackoffFunc func(attempt int) time.Duration

BackoffFunc returns the backoff duration for a retry attempt.

func ExponentialBackoff

func ExponentialBackoff(base, max time.Duration) BackoffFunc

ExponentialBackoff returns a backoff function with exponential growth.

type DeadLetter

type DeadLetter struct {
	Name     string
	Attempts int
	Err      error
}

DeadLetter captures a job that failed permanently.

type Handler

type Handler func(context.Context) error

Handler executes a background job.

type Job

type Job struct {
	Name         string
	Handler      Handler
	Context      context.Context
	Retry        *RetryPolicy
	Timeout      time.Duration
	OnRetry      func(RetryInfo)
	OnDeadLetter func(DeadLetter)
}

Job describes a background task.

type Options

type Options struct {
	Workers      int
	QueueSize    int
	Retry        *RetryPolicy
	Timeout      time.Duration
	OnRetry      func(RetryInfo)
	OnDeadLetter func(DeadLetter)
	Sleep        func(time.Duration)
}

Options configures a Runner.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns a Runner configuration with sane defaults.

type RetryDecider

type RetryDecider func(err error) bool

RetryDecider decides whether a retry should be attempted.

type RetryInfo

type RetryInfo struct {
	Name      string
	Attempt   int
	Err       error
	NextDelay time.Duration
}

RetryInfo describes a retry attempt.

type RetryPolicy

type RetryPolicy struct {
	MaxRetries int
	Backoff    BackoffFunc
	RetryIf    RetryDecider
}

RetryPolicy defines retry behavior.

func DefaultRetryPolicy

func DefaultRetryPolicy() RetryPolicy

DefaultRetryPolicy returns a retry configuration with exponential backoff.

type Runner

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

Runner manages background jobs.

func New

func New(options Options) *Runner

New creates a new Runner.

func (*Runner) Enqueue

func (r *Runner) Enqueue(job Job) error

Enqueue schedules a job for execution.

func (*Runner) EnqueueContext

func (r *Runner) EnqueueContext(ctx context.Context, job Job) error

EnqueueContext schedules a job, honoring context cancellation while waiting.

func (*Runner) Shutdown

func (r *Runner) Shutdown(ctx context.Context) error

Shutdown stops accepting new jobs and waits for workers to finish.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context)

Start launches worker goroutines.

Jump to

Keyboard shortcuts

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