Documentation
¶
Overview ¶
Package jobs runs the web UI's long operations -- syncs, recipes, audits -- in the background, records what each one did as a stream of typed events, and keeps a bounded history on disk. It knows nothing about HTTP.
Index ¶
- Constants
- Variables
- func Handler(j *Job, level slog.Level) slog.Handler
- func Observer(j *Job) pipeline.Observer
- func ValidID(id string) bool
- type ErrBusy
- type Event
- type Info
- type Job
- func (j *Job) Done() <-chan struct{}
- func (j *Job) Events() []Event
- func (j *Job) Info() Info
- func (j *Job) Logf(level, format string, a ...any)
- func (j *Job) PackDone(pack, status, errText, detail string)
- func (j *Job) PackStarted(pack, detail string)
- func (j *Job) Record(v any) error
- func (j *Job) Result() any
- func (j *Job) SetResult(v any)
- func (j *Job) Since(id int64) ([]Event, bool, <-chan struct{})
- type Kind
- type Registry
- type RunFunc
- type Spec
- type Status
- type Store
Constants ¶
const ( EventLog = "log" EventStage = "stage" EventPackStart = "pack-start" EventPackDone = "pack-done" EventDone = "done" // EventResult carries a kind-specific payload in Data -- the audit's // resolutions -- so it is kept with the run and survives a restart. EventResult = "result" )
Event types.
const ( StageStarted = "started" StageFinished = "finished" StageFailed = "failed" )
Stage states, on EventStage.
const DefaultCapacity = 10000
DefaultCapacity is how many events a job keeps in memory: enough for an aggregator pack's dozens of sets, a ceiling against a runaway logger.
const DefaultKeep = 50
DefaultKeep is how many finished jobs the history keeps. It is a ceiling: a test pins it, so raising it is a decision rather than drift.
Variables ¶
var ErrNotFound = errors.New("no such job")
ErrNotFound is returned for a job ID this process and the history lack.
Functions ¶
func Handler ¶
Handler returns a slog.Handler that records every line into the job, at level and above. Passed to a run as its logger, it is what keeps two concurrent runs' logs apart.
Types ¶
type Event ¶
type Event struct {
ID int64 `json:"id"`
Type string `json:"type"`
Time time.Time `json:"time"`
// log
Level string `json:"level,omitempty"`
Msg string `json:"msg,omitempty"`
Attrs map[string]string `json:"attrs,omitempty"`
// stage
Pipeline string `json:"pipeline,omitempty"`
Stage string `json:"stage,omitempty"`
State string `json:"state,omitempty"`
Metrics map[string]any `json:"metrics,omitempty"`
// pack-start, pack-done, done
Pack string `json:"pack,omitempty"`
Status string `json:"status,omitempty"`
Err string `json:"error,omitempty"`
// result
Data json.RawMessage `json:"data,omitempty"`
}
Event is one thing that happened in a job. ID increases by one per event within a job, so a reconnecting EventSource resumes exactly where it left off with Last-Event-ID.
type Info ¶
type Info struct {
ID string `json:"id"`
Kind Kind `json:"kind"`
Packs []string `json:"packs"`
DryRun bool `json:"dry_run"`
Label string `json:"label,omitempty"`
Started time.Time `json:"started"`
Ended time.Time `json:"ended,omitzero"`
Status Status `json:"status"`
Err string `json:"error,omitempty"`
}
Info is a job's metadata, as listed and persisted.
type Job ¶
type Job struct {
// contains filtered or unexported fields
}
Job is one background operation.
func (*Job) Done ¶
func (j *Job) Done() <-chan struct{}
Done is closed when the job has finished and its done event is recorded.
func (*Job) PackStarted ¶
PackStarted and PackDone record a pack's boundaries.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry starts jobs, enforces one job per pack, and keeps them findable.
func NewRegistry ¶
NewRegistry returns a Registry. store may be nil (no history on disk); base is cancelled to stop every job, as on shutdown.
func (*Registry) List ¶
List returns this process's jobs and the persisted history, newest first, each ID once.
func (*Registry) SetCapacity ¶
SetCapacity sets the per-job event capacity; for tests.
type RunFunc ¶
RunFunc does the work. It must honour ctx; the status it returns is the job's outcome unless ctx was cancelled, which always reads as cancelled.
type Spec ¶
type Spec struct {
Kind Kind
Packs []string // the packs it touches, locked for its duration
DryRun bool
// Label is a one-line description for the history, e.g. "recipe 3.49.0".
Label string
}
Spec describes a job to start.
type Store ¶
Store persists finished jobs as JSON Lines, one file per job: the first line is the job's Info, every later line one Event. It only ever touches files in its own directory whose names it could have written.