task

package
v1.13.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MPL-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package task tracks detached background work so its status can be polled independently of the CLI invocation that started it.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCanceled  = errors.New("canceled")
	ErrAbandoned = errors.New("worker exited without recording a result")
)

Functions

This section is empty.

Types

type CreateOptions

type CreateOptions struct {
	Command     string
	WorkspaceID string
}

CreateOptions labels a task at creation time for later listing.

type State

type State struct {
	ID          string         `json:"id"`
	Command     string         `json:"command,omitempty"`
	WorkspaceID string         `json:"workspaceId,omitempty"`
	Status      Status         `json:"status"`
	Phase       string         `json:"phase,omitempty"`
	Step        string         `json:"step,omitempty"`
	Error       string         `json:"error,omitempty"`
	Result      *config.Result `json:"result,omitempty"`
	PID         int            `json:"pid,omitempty"`
	StartedAt   time.Time      `json:"startedAt"`
	UpdatedAt   time.Time      `json:"updatedAt"`
}

State is the JSON snapshot persisted for a task. PID names the OS process doing the work, distinct from whatever process is merely polling this state.

type Status

type Status string
const (
	StatusPending   Status = "pending"
	StatusRunning   Status = "running"
	StatusSucceeded Status = "succeeded"
	StatusFailed    Status = "failed"
)

func (Status) Terminal

func (s Status) Terminal() bool

type Store

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

Store persists task state as one JSON file per task under dir.

func NewStore

func NewStore() (*Store, error)

func NewStoreAt

func NewStoreAt(dir string) (*Store, error)

func (*Store) Abandoned

func (s *Store) Abandoned(state *State) bool

Abandoned reports whether a non-terminal task's worker is gone.

func (*Store) Create

func (s *Store) Create(opts CreateOptions) (*Task, error)

func (*Store) Delete

func (s *Store) Delete(id string, force bool) error

Delete errors if the task is still pending or running unless force is set.

func (*Store) Get

func (s *Store) Get(id string) (*State, error)

func (*Store) List

func (s *Store) List() ([]*State, error)

List returns every known task, most recently started first.

func (*Store) Open

func (s *Store) Open(id string) *Task

Open returns a handle without reading the task's state.

func (*Store) Reconcile

func (s *Store) Reconcile(state *State) *State

Reconcile marks a task failed when its worker died without recording a result, so it stops being reported as still running. Returns the effective state, which is unchanged for live and already-terminal tasks.

type Task

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

Task is a handle to a single background task, bound to the Store it was created in. Report may be called from multiple goroutines; each call serializes its own read-modify-write of the state file.

func (*Task) Cancel

func (t *Task) Cancel() error

Cancel is safe to call even if the process already exited on its own. The terminal check and state transition happen atomically under the same lock, so a concurrent report from the task's own worker can't race with marking it canceled here.

func (*Task) Fail

func (t *Task) Fail(err error) error

Fail preserves an existing terminal state, so the error a canceled worker reports on its way out doesn't mask ErrCanceled as the reason it stopped.

func (*Task) HoldWorkerLock

func (t *Task) HoldWorkerLock() error

HoldWorkerLock claims this task's worker lock for the rest of the process's life, marking it as actively being worked on. Callers must not release it: the kernel does so when the process exits, crashes, or is killed.

Returns an error if another process already holds the lock, since that means a worker for this task is already running.

func (*Task) ID

func (t *Task) ID() string

func (*Task) ReleaseWorkerLockForTest

func (t *Task) ReleaseWorkerLockForTest() error

ReleaseWorkerLockForTest drops the lock HoldWorkerLock acquired, letting a test simulate a dead worker. Production code must never call it.

Not in export_test.go: other packages' tests need it, and a _test.go file is only compiled into its own package's test binary.

func (*Task) Reporter

func (t *Task) Reporter() status.Reporter

func (*Task) SetPID

func (t *Task) SetPID(pid int) error

func (*Task) SetWorkspaceID

func (t *Task) SetWorkspaceID(id string) error

SetWorkspaceID corrects the task's workspace label to the resolved ID, which may differ from whatever label it was created with (e.g. a raw source string guessed before workspace resolution ran). client.Status looks tasks up by this label, so it must end up accurate.

func (*Task) Succeed

func (t *Task) Succeed(result *config.Result) error

Succeed is a no-op once the task is terminal, so a worker that finishes concurrently with a Cancel can't overwrite the canceled state with success.

Jump to

Keyboard shortcuts

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