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 ¶
- Variables
- func CompletePendingTx(ctx context.Context, tx *sql.Tx, id string, now time.Time) (bool, error)
- func RunPool(ctx context.Context, queue *Queue, handlers map[string]Handler, ...) error
- func Stop(err error) error
- type Handler
- type Intent
- type PoolOptions
- type Queue
- func (q *Queue) Cancel(ctx context.Context, id string, now time.Time) error
- func (q *Queue) CancelClaim(ctx context.Context, id, claimToken string, cause error, now time.Time) (bool, error)
- func (q *Queue) Claim(ctx context.Context, now time.Time, lease time.Duration) (*Intent, error)
- func (q *Queue) Complete(ctx context.Context, id, claimToken string, now time.Time) (bool, error)
- func (q *Queue) Enqueue(ctx context.Context, intent Intent) (string, error)
- func (q *Queue) List(ctx context.Context, state string) ([]Intent, error)
- func (q *Queue) Metrics() prometheus.Collector
- func (q *Queue) Pending(ctx context.Context, kind string) ([]Intent, error)
- func (q *Queue) Renew(ctx context.Context, id, claimToken string, now time.Time, lease time.Duration) (bool, error)
- func (q *Queue) Retry(ctx context.Context, id, claimToken string, cause error, now time.Time) (bool, error)
- func (q *Queue) RetryNow(ctx context.Context, id string, now time.Time) error
- type Worker
- type WorkerOptions
Constants ¶
This section is empty.
Variables ¶
var ErrClaimLost = errors.New("work: claim lost")
ErrClaimLost denotes loss of worker lease ownership.
Functions ¶
func CompletePendingTx ¶
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.
Types ¶
type Handler ¶
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 (*Queue) Cancel ¶
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) Complete ¶
Complete marks a running intent complete if the token still fences the caller.
func (*Queue) Metrics ¶
func (q *Queue) Metrics() prometheus.Collector
Metrics returns the per-kind pending gauge collector.
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.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker owns a queue lifecycle and processes one intent at a time.
func NewWorkerWithOptions ¶
func NewWorkerWithOptions(queue *Queue, handlers map[string]Handler, options WorkerOptions) *Worker
NewWorkerWithOptions creates a worker with explicit lease behavior.