work

package
v0.0.0-...-9268282 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package work provides durable, fenced work intents backed by a storage.Store.

Queue.Enqueue is idempotent by intent ID. Claim selects the oldest due pending intent, or a running intent whose lease expired, and assigns a random claim token. Renew, Complete, Retry and CancelClaim update an intent only while that token owns the claim, fencing updates from earlier attempts.

Worker invokes the handler for a claim while renewing its lease. A nil or missing handler is an ordinary failure and is retried. Handler errors are retried with deterministic jittered exponential backoff; Stop marks the intent cancelled and records the underlying error. If lease renewal fails, the handler context is cancelled and its result is discarded as fenced. RunPool starts the requested number of workers, defaults to four, and stops the pool when its context is cancelled or a queue operation fails.

Index

Constants

This section is empty.

Variables

View Source
var ErrClaimLost = errors.New("work: claim lost")

ErrClaimLost denotes loss of worker lease ownership.

Functions

func CompletePendingTx

func CompletePendingTx(ctx context.Context, tx *sql.Tx, id string, now time.Time) (bool, error)

CompletePendingTx marks a pending intent complete in the caller's transaction. Running intents are left untouched so an active worker keeps ownership of its claim.

func RunPool

func RunPool(ctx context.Context, queue *Queue, handlers map[string]Handler, options PoolOptions) error

RunPool runs a worker pool until ctx is cancelled or a worker reports a queue error. The first queue error cancels the remaining workers and is returned after they have stopped.

func Stop

func Stop(err error) error

Stop marks a handler failure as terminal. Workers cancel the claimed intent instead of retrying it, while retaining the underlying error for operators.

Types

type Handler

type Handler func(ctx context.Context, intent Intent) error

Handler processes one claimed intent while the worker renews its lease. A nil error completes the claim; an error schedules a retry. Stop marks a terminal failure. The handler observes ctx cancellation so the worker can finish shutdown or release an attempt whose claim was lost. A retry can repeat an external effect after an interrupted acknowledgment; handlers own deduplication of those effects.

type Intent

type Intent struct {
	ID         string
	Kind       string
	EventID    string
	Target     string
	Payload    string
	State      string
	Attempts   int
	NextAt     time.Time
	CreatedAt  time.Time
	UpdatedAt  time.Time
	LastError  string
	ClaimToken string
	ClaimUntil time.Time
}

Intent is a durable unit of work. ClaimToken identifies the current claim; ClaimUntil determines when another worker can claim an expired lease. State is pending, running, completed or cancelled.

type PoolOptions

type PoolOptions struct {
	Workers int
	Worker  WorkerOptions
}

PoolOptions controls a pool of durable workers. Workers less than or equal to zero use the default of four.

type Queue

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

Queue implements durable claiming and fencing over a storage Store.

func New

func New(store *storage.Store) *Queue

New creates a queue. Metrics returns a collector that hosts may register.

func (*Queue) Cancel

func (q *Queue) Cancel(ctx context.Context, id string, now time.Time) error

Cancel prevents future claims. A running claim can be cancelled administratively.

func (*Queue) CancelClaim

func (q *Queue) CancelClaim(ctx context.Context, id, claimToken string, cause error, now time.Time) (bool, error)

CancelClaim marks a running intent terminal if the claim token still fences the caller.

func (*Queue) Claim

func (q *Queue) Claim(ctx context.Context, now time.Time, lease time.Duration) (*Intent, error)

Claim atomically claims the oldest due pending or expired intent.

func (*Queue) Complete

func (q *Queue) Complete(ctx context.Context, id, claimToken string, now time.Time) (bool, error)

Complete marks a running intent complete if the token still fences the caller.

func (*Queue) Enqueue

func (q *Queue) Enqueue(ctx context.Context, intent Intent) (string, error)

Enqueue inserts an intent idempotently.

func (*Queue) List

func (q *Queue) List(ctx context.Context, state string) ([]Intent, error)

List returns intents in the requested state; an empty state lists all states.

func (*Queue) Metrics

func (q *Queue) Metrics() prometheus.Collector

Metrics returns the per-kind pending gauge collector.

func (*Queue) Pending

func (q *Queue) Pending(ctx context.Context, kind string) ([]Intent, error)

Pending returns pending and running intents, optionally filtered by kind.

func (*Queue) Renew

func (q *Queue) Renew(ctx context.Context, id, claimToken string, now time.Time, lease time.Duration) (bool, error)

Renew extends a claim if the token still fences the caller.

func (*Queue) Retry

func (q *Queue) Retry(ctx context.Context, id, claimToken string, cause error, now time.Time) (bool, error)

Retry returns a failed claim to pending with unbounded exponential backoff.

func (*Queue) RetryNow

func (q *Queue) RetryNow(ctx context.Context, id string, now time.Time) error

RetryNow makes an intent immediately eligible, regardless of its current pending delay.

type Worker

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

Worker owns a queue lifecycle and processes one intent at a time.

func NewWorker

func NewWorker(queue *Queue, handlers map[string]Handler) *Worker

NewWorker creates a worker with copied handlers.

func NewWorkerWithOptions

func NewWorkerWithOptions(queue *Queue, handlers map[string]Handler, options WorkerOptions) *Worker

NewWorkerWithOptions creates a worker with explicit lease behavior.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context) error

Run blocks until the context is cancelled or a durable queue error occurs.

type WorkerOptions

type WorkerOptions struct {
	Lease   time.Duration
	Renew   time.Duration
	Observe func(kind, outcome string, duration time.Duration)
}

WorkerOptions controls claim leases and optional handler timing observation. Lease and Renew default to 30 seconds and one third of the lease.

Jump to

Keyboard shortcuts

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