fleet

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package fleet implements an in-memory task board that workers pull from. Every operation runs under a single mutex, so each task is claimed by at most one worker at a time. Claims are served FIFO, and failed tasks can be requeued for retry.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound          = errors.New("fleet: task not found")
	ErrIllegalTransition = errors.New("fleet: illegal state transition")
)

Sentinel errors returned by Board methods.

Functions

This section is empty.

Types

type Board

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

Board is an in-memory task board. Construct with NewBoard or Seed. All methods are safe for concurrent use.

func Load

func Load(tasks []Task, lease time.Duration) *Board

Load reconstructs a board from an Export snapshot, preserving IDs, states, and insertion order.

func NewBoard

func NewBoard() *Board

NewBoard returns an empty board ready for use.

func Seed

func Seed(payloads []string) *Board

Seed returns a new board with one pending task per payload, added in order.

func (*Board) Add

func (b *Board) Add(payload string) *Task

Add enqueues a new pending task with the given payload and returns a copy.

func (*Board) Claim

func (b *Board) Claim(owner string) (Task, bool)

Claim takes the oldest pending task, marks it claimed by owner, and returns a copy. It returns false if nothing is pending. The scan-and-mutate runs under the board mutex, so two callers can never get the same task.

func (*Board) Complete

func (b *Board) Complete(id, owner, result string) error

Complete marks the claimed task id as done with the given result. owner must match the worker that claimed the task (empty owner skips the check, for trusted/local callers).

func (*Board) Export

func (b *Board) Export() []Task

Export returns a copy of every task in insertion order, suitable for serialization.

func (*Board) Fail

func (b *Board) Fail(id, owner, errMsg string, requeue bool) error

Fail marks the claimed task id as failed with errMsg. When requeue is true the task instead goes back to the pending pool (owner cleared, attempts kept) so another worker can retry it. owner must match the claiming worker (empty owner skips the check, for trusted/local callers).

func (*Board) Get

func (b *Board) Get(id string) (Task, bool)

Get returns a copy of the task with the given ID, if it exists.

func (*Board) Heartbeat

func (b *Board) Heartbeat(id, owner string) (Task, error)

Heartbeat extends the lease on a claimed task held by owner. It fails if the task doesn't exist, isn't claimed, or is claimed by someone else.

func (*Board) List

func (b *Board) List() []Task

List returns a snapshot copy of every task in insertion order.

func (*Board) Remaining

func (b *Board) Remaining() int

Remaining returns the number of tasks still in flight: pending plus claimed.

func (*Board) SetLease

func (b *Board) SetLease(d time.Duration)

SetLease sets the claim lease duration. When d > 0, Claim stamps a deadline of now+d, Heartbeat extends it, and Sweep requeues tasks whose lease has passed. Non-positive d disables lease expiry.

func (*Board) Stats

func (b *Board) Stats() Stats

Stats returns a point-in-time count of tasks by state.

func (*Board) Sweep

func (b *Board) Sweep(now time.Time) []Task

Sweep requeues every claimed task whose lease expired as of now, so a dead worker's task returns to the pending pool. Attempts are retained. It returns copies of the requeued tasks; tasks with no lease are never swept.

type Stats

type Stats struct {
	Total   int `json:"total"`
	Pending int `json:"pending"`
	Claimed int `json:"claimed"`
	Done    int `json:"done"`
	Failed  int `json:"failed"`
}

Stats is a point-in-time count of tasks by state.

type Task

type Task struct {
	ID      string    `json:"id"`
	Payload string    `json:"payload"`
	State   TaskState `json:"state"`
	// Owner is the worker that last claimed the task; cleared on requeue.
	Owner string `json:"owner"`
	// Attempts counts claims, not completions.
	Attempts  int       `json:"attempts"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Result    string    `json:"result"`
	Error     string    `json:"error"`
	// LeaseExpiry is when the current claim expires; Sweep requeues claimed
	// tasks past it. Zero means no lease (claims never expire).
	LeaseExpiry time.Time `json:"lease_expiry,omitempty"`
}

Task is a unit of work on the board. Board methods hand out copies, so callers can never mutate board state through a returned value.

type TaskState

type TaskState string

TaskState is the lifecycle state of a Task.

const (
	StatePending TaskState = "pending"
	StateClaimed TaskState = "claimed"
	StateDone    TaskState = "done"
	StateFailed  TaskState = "failed"
)

A task starts pending, becomes claimed when a worker takes it, and finishes done or failed. A failed task may be requeued back to pending.

Jump to

Keyboard shortcuts

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