task

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package task defines the core Task record and its lifecycle.

Index

Constants

View Source
const DefaultMaxAttempts = 3

DefaultMaxAttempts is used when New.MaxAttempts is zero.

Variables

View Source
var (
	// ErrNotFound is returned when a task ID does not exist.
	ErrNotFound = errors.New("task: not found")
	// ErrLeaseNotHeld is returned when completing/failing/heartbeating a task
	// whose lease is owned by someone else or already expired.
	ErrLeaseNotHeld = errors.New("task: lease not held")
	// ErrDuplicateID is returned when enqueueing a task whose ID already exists.
	ErrDuplicateID = errors.New("task: duplicate id")
	// ErrInvalidTransition is returned by stores that validate transitions.
	ErrInvalidTransition = errors.New("task: invalid status transition")
)

Functions

func CanTransitionTo

func CanTransitionTo(from, to Status) bool

CanTransitionTo reports whether from → to is a legal lifecycle transition.

func Terminal

func Terminal(s Status) bool

Terminal reports whether s is a terminal status.

Types

type ID

type ID string

ID identifies a task. Opaque, unique, roughly time-sortable.

func NewID

func NewID() ID

NewID returns a new unique task ID (timestamp prefix + random suffix).

func (ID) String

func (id ID) String() string

String returns the raw ID.

type New

type New struct {
	Project     string
	Type        string
	Payload     json.RawMessage
	Deps        []ID
	Priority    int
	MaxAttempts int
	NotBefore   time.Time
	// DedupKey, when set, makes Enqueue idempotent: if a task with the same
	// key already exists, that task is returned unchanged and no duplicate is
	// created. Use a stable derivation (e.g. hash of project + source + title)
	// so repeated harvest runs converge instead of re-enqueueing.
	DedupKey string
}

New is a task template for enqueueing. ID, Attempts, Status and timestamps are assigned by the store; everything else is caller-supplied.

func (New) Normalize

func (n New) Normalize() New

Normalize applies defaults to a template.

type Status

type Status string

Status is the task lifecycle state. Transitions are validated by CanTransitionTo.

const (
	// Pending means the task is waiting to be claimed (possibly delayed via NotBefore).
	Pending Status = "pending"
	// Running means a worker holds the lease.
	Running Status = "running"
	// Completed means the task succeeded (terminal).
	Completed Status = "completed"
	// Dead means attempts were exhausted and the task is parked in the dead-letter queue (terminal).
	Dead Status = "dead"
	// Cancelled means the task was withdrawn before completion (terminal).
	Cancelled Status = "cancelled"
)

func (Status) Valid

func (s Status) Valid() bool

Valid reports whether s is a known status value.

type Task

type Task struct {
	ID           ID              `json:"id"`
	Project      string          `json:"project,omitempty"`
	Type         string          `json:"type"`
	Payload      json.RawMessage `json:"payload,omitempty"`
	Deps         []ID            `json:"deps,omitempty"`
	Priority     int             `json:"priority,omitempty"`
	Attempts     int             `json:"attempts"`
	MaxAttempts  int             `json:"maxAttempts"`
	NotBefore    time.Time       `json:"notBefore"`
	Status       Status          `json:"status"`
	LeaseOwner   string          `json:"leaseOwner,omitempty"`
	LeaseExpires *time.Time      `json:"leaseExpires,omitempty"`
	LastError    string          `json:"lastError,omitempty"`
	CreatedAt    time.Time       `json:"createdAt"`
	UpdatedAt    time.Time       `json:"updatedAt"`
	CompletedAt  *time.Time      `json:"completedAt,omitempty"`
}

Task is the unit of work: what to run, for which project, under which constraints.

Jump to

Keyboard shortcuts

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