jobs

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jobs runs a change that takes longer than a request, or reaches another machine, as a job: one at a time, under the advisory lock for as long as it runs, with its output kept line by line for a page to stream.

A job is a person's confirmed command, run exactly as the CLI runs it. It can pause to ask the person something (rotate's "pasted the key?"), and it can be cancelled, which stops it between steps - never inside one, so a cancelled job never leaves a step half done. It holds the process's one change slot (mutation.Exclusive) from start to end, so no preview, change or other job interleaves with it.

Index

Constants

View Source
const (
	Running   = "running"
	Waiting   = "waiting" // paused on a question
	Done      = "done"
	Failed    = "failed"
	Cancelled = "cancelled"
)

States a job is in.

View Source
const (
	ItemWaiting = "waiting"
	ItemRunning = "running"
	ItemDone    = "done"
	ItemFailed  = "failed"
)

Item states, as a job page shows them.

Variables

View Source
var ErrCancelled = fmt.Errorf("cancelled: the steps before this one finished, and nothing after it ran: %w",
	mutation.ErrDeclined)

ErrCancelled is what a job's steps return once it has been cancelled. The history records the job as aborted.

Functions

This section is empty.

Types

type ErrBusy

type ErrBusy struct{ ID, Command string }

ErrBusy names the job already running.

func (ErrBusy) Error

func (e ErrBusy) Error() string

type Item added in v0.1.6

type Item struct{ Name, State string }

Item is one thing a job works through, and how far it has got.

type Job

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

Job is one running or finished job.

func (*Job) Ask

func (j *Job) Ask(prompt, text string) bool

Ask pauses the job until the person answers, and reports their answer. A cancel answers no.

func (*Job) AskWith added in v0.1.6

func (j *Job) AskWith(q Question) bool

AskWith is Ask with the question's own answer labels.

func (*Job) Failed added in v0.1.6

func (j *Job) Failed(item string)

Failed marks one of the planned items as failed; the rest go on.

func (*Job) Plan added in v0.1.6

func (j *Job) Plan(items []string)

Plan names the things the job will work through, in order, so its page can list them one per row: waiting, running, done or failed. Progress moves through them.

func (*Job) Progress added in v0.1.6

func (j *Job) Progress(done, total int, label string)

Progress records that done of total steps are finished and label is the one under way, for the job's page to show as a bar with the time left. A job whose work is one step does not call it.

func (*Job) SetSummary added in v0.1.6

func (j *Job) SetSummary(v any)

SetSummary gives the job a result for its page to draw as its output; a job may set it again as it goes, and its page draws it again.

func (*Job) SetTitle added in v0.1.6

func (j *Job) SetTitle(title string)

SetTitle names the job for people, as its review did: "Rotate 10 keys".

func (*Job) Step

func (j *Job) Step() error

Step reports ErrCancelled once the job has been cancelled; a job calls it between its steps.

func (*Job) View

func (j *Job) View() (View, <-chan struct{})

View is the job as it is now, and a channel closed at its next change.

func (*Job) Write

func (j *Job) Write(b []byte) (int, error)

Write appends output, one line at a time, so a page can stream it.

type Progress added in v0.1.6

type Progress struct {
	Done, Total int
	Label       string
	Since       time.Time
	// Items are the things the job works through in order (keys, hosts),
	// each with its state, when the job named them with Plan.
	Items []Item
}

Progress is how far a job made of counted steps has got: Done of Total steps finished, Label naming the one under way, and Since when the first step began, from which a page works out the time left.

func (Progress) Left added in v0.1.6

func (p Progress) Left(now time.Time) (time.Duration, bool)

Left is the time the remaining steps should take at the pace of the finished ones, and false while no step has finished to judge by.

func (Progress) Percent added in v0.1.6

func (p Progress) Percent() int

Percent is how much of the job is done, 0 to 100.

type Question

type Question struct {
	Prompt string
	Text   string
	// Yes and No label the two answers when the question is not "is the
	// step done?" (pinning asks whether to trust keys). Empty keeps the
	// default wording.
	Yes, No string
}

Question is what a paused job is asking. Text is shown as is, for copying: rotate's staged public key.

type Registry

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

Registry holds the jobs a server has started, and allows one to run at a time.

func NewRegistry

func NewRegistry() *Registry

NewRegistry is an empty registry.

func (*Registry) Answer

func (r *Registry) Answer(id string, yes bool) bool

Answer answers the question a job is waiting on. It reports false when the job is not waiting.

func (*Registry) Cancel

func (r *Registry) Cancel(id string) bool

Cancel stops a job at its next step, answering no to a question it waits on.

func (*Registry) Get

func (r *Registry) Get(id string) *Job

Get is the job with id, or nil.

func (*Registry) Running

func (r *Registry) Running() *Job

Running is the job running now, or nil.

func (*Registry) Start

func (r *Registry) Start(p paths.Paths, scope mutation.Scope, run func(j *Job) error) (*Job, error)

Start runs run as a job on p, under the lock, as one operation in the history. It refuses while another job runs, and waits briefly for a preview or change in flight. The job runs on after Start returns.

type View

type View struct {
	ID, Command, State, Err, OpID string
	// Title names the job for people ("Rotate 10 keys"); "" for a job that
	// was given none, whose page then shows its command.
	Title    string
	Args     []string
	Lines    []string
	Question *Question
	Started  time.Time
	// Progress is nil for a job that does not count its steps.
	Progress *Progress
	// Summary is the job's result for its page to draw (a rotation's grouped
	// summary), kept current as the job runs; nil for most jobs.
	// SummaryVersion counts its changes, so a page knows to draw it again.
	Summary        any
	SummaryVersion int
}

View is a job as a page shows it.

Jump to

Keyboard shortcuts

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