task

package
v0.3.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package task defines Modary's bounded durable background-work contract.

The official PostgreSQL adapter implements this contract with River, but consumers do not depend on River or PostgreSQL types. Delivery is at least once: handlers must use stable job identity and idempotent external effects.

Index

Constants

View Source
const (
	// MaxPayloadBytes bounds one serialized task payload.
	MaxPayloadBytes = 1 << 20
	// DefaultQueue is used when Request.Queue is empty.
	DefaultQueue = "default"
	// DefaultMaxAttempts is used when Request.MaxAttempts is zero.
	DefaultMaxAttempts = 3
	// DefaultMaxWorkers is used when Queue.MaxWorkers is zero.
	DefaultMaxWorkers = 10
	// MinimumRetryDelay keeps a custom retry scheduled strictly in the future.
	MinimumRetryDelay = time.Millisecond
	// MaximumRetryDelay bounds declarative per-runner retry configuration.
	MaximumRetryDelay = 30 * 24 * time.Hour
	// DefaultListLimit bounds one operational inspection page by default.
	DefaultListLimit = 50
	// MaxListLimit is the largest operational inspection page.
	MaxListLimit = 100
)

Variables

View Source
var (
	// ErrUnavailable reports use before installation or after shutdown.
	ErrUnavailable = errors.New("task service is unavailable")
	// ErrTransactionRequired reports enqueue outside a governed Action transaction.
	ErrTransactionRequired = errors.New("task enqueue requires a governed transaction")
)

Functions

This section is empty.

Types

type Handler

type Handler interface {
	Handle(context.Context, Job) error
}

Handler performs one at-least-once task attempt.

type HandlerFunc

type HandlerFunc func(context.Context, Job) error

HandlerFunc adapts a function to Handler.

func (HandlerFunc) Handle

func (function HandlerFunc) Handle(ctx context.Context, job Job) error

Handle invokes function.

type Inspector

type Inspector interface {
	List(context.Context, ListOptions) (Page, error)
}

Inspector reads bounded task metadata without exposing a queue backend or mutation authority.

type Job

type Job struct {
	ID          int64
	Kind        string
	Payload     json.RawMessage
	Queue       string
	Attempt     int
	MaxAttempts int
}

Job is the framework-neutral task value supplied to a Handler.

func (Job) TerminalAttempt

func (job Job) TerminalAttempt() bool

TerminalAttempt reports whether the current attempt is the last configured attempt. It is useful when a product must persist its own terminal status.

type ListOptions

type ListOptions struct {
	Limit    int
	BeforeID int64
	Queue    string
	State    State
}

ListOptions selects one descending task page. BeforeID is the exclusive cursor returned by the previous Page.

func NormalizeListOptions

func NormalizeListOptions(options ListOptions) (ListOptions, error)

NormalizeListOptions validates one provider-neutral inspection query.

type Page

type Page struct {
	Tasks        []Summary `json:"tasks"`
	NextBeforeID int64     `json:"next_before_id,omitempty,string"`
}

Page is one bounded operational task result.

type Queue

type Queue struct {
	Name       string
	MaxWorkers int
}

Queue declares one runner queue and its per-process concurrency.

type Receipt

type Receipt struct {
	ID                  int64
	DuplicateSuppressed bool
}

Receipt identifies the durable job selected by an enqueue operation.

type Request

type Request struct {
	Kind        string
	Payload     json.RawMessage
	Queue       string
	MaxAttempts int
	ScheduledAt time.Time
	UniqueKey   string
}

Request describes one durable task insertion. UniqueKey is optional. When present, an equivalent logical kind and key are inserted at most once while River's active uniqueness states apply.

func NormalizeRequest

func NormalizeRequest(request Request) (Request, error)

NormalizeRequest validates and defensively copies one enqueue request. It is public so adapter and consumer contract tests use the same rules.

type Runner

type Runner interface {
	Start(context.Context) error
	Stop(context.Context) error
	Stopped() <-chan struct{}
}

Runner owns one immutable worker process lifecycle.

type RunnerOptions

type RunnerOptions struct {
	Queues          []Queue
	JobTimeout      time.Duration
	SoftStopTimeout time.Duration
	// RetryDelays optionally replaces River's default retry schedule. Entry zero
	// follows the first failed attempt; attempts beyond the list reuse its last
	// value. An empty list selects the adapter default.
	RetryDelays []time.Duration
}

RunnerOptions freezes runner behavior before it starts.

func NormalizeRunnerOptions

func NormalizeRunnerOptions(options RunnerOptions) (RunnerOptions, error)

NormalizeRunnerOptions validates and defensively copies runner options.

type Service

type Service interface {
	Enqueue(context.Context, Request) (Receipt, error)
	NewRunner(Handler, RunnerOptions) (Runner, error)
}

Service inserts tasks and constructs immutable runners.

type State

type State string

State is the provider-neutral lifecycle state exposed by task inspection. Queue implementations map their internal states into this closed contract.

const (
	// StateQueued is ready for a worker to claim.
	StateQueued State = "queued"
	// StatePending is accepted but not yet eligible to run.
	StatePending State = "pending"
	// StateScheduled is waiting for its scheduled time.
	StateScheduled State = "scheduled"
	// StateRunning is currently executing.
	StateRunning State = "running"
	// StateRetrying is waiting for another attempt after failure.
	StateRetrying State = "retrying"
	// StateSucceeded completed successfully.
	StateSucceeded State = "succeeded"
	// StateFailed reached a terminal failure.
	StateFailed State = "failed"
	// StateCancelled was cancelled before successful completion.
	StateCancelled State = "cancelled"
)

func (State) Valid

func (state State) Valid() bool

Valid reports whether state belongs to the public inspection contract.

type Summary

type Summary struct {
	ID          int64      `json:"id,string"`
	Kind        string     `json:"kind"`
	Queue       string     `json:"queue"`
	State       State      `json:"state"`
	Attempt     int        `json:"attempt"`
	MaxAttempts int        `json:"max_attempts"`
	ScheduledAt time.Time  `json:"scheduled_at"`
	CreatedAt   time.Time  `json:"created_at"`
	FinalizedAt *time.Time `json:"finalized_at,omitempty"`
}

Summary is provider-neutral operational task metadata. Payloads and backend error details are deliberately excluded from the Admin inspection surface.

Jump to

Keyboard shortcuts

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