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
- Variables
- type ErrBusy
- type Item
- type Job
- func (j *Job) Ask(prompt, text string) bool
- func (j *Job) AskWith(q Question) bool
- func (j *Job) Failed(item string)
- func (j *Job) Plan(items []string)
- func (j *Job) Progress(done, total int, label string)
- func (j *Job) SetSummary(v any)
- func (j *Job) SetTitle(title string)
- func (j *Job) Step() error
- func (j *Job) View() (View, <-chan struct{})
- func (j *Job) Write(b []byte) (int, error)
- type Progress
- type Question
- type Registry
- type View
Constants ¶
const ( Running = "running" Waiting = "waiting" // paused on a question Done = "done" Failed = "failed" Cancelled = "cancelled" )
States a job is in.
const ( ItemWaiting = "waiting" ItemRunning = "running" ItemDone = "done" ItemFailed = "failed" )
Item states, as a job page shows them.
Variables ¶
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 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 ¶
Ask pauses the job until the person answers, and reports their answer. A cancel answers no.
func (*Job) Failed ¶ added in v0.1.6
Failed marks one of the planned items as failed; the rest go on.
func (*Job) Plan ¶ added in v0.1.6
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
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
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
SetTitle names the job for people, as its review did: "Rotate 10 keys".
func (*Job) Step ¶
Step reports ErrCancelled once the job has been cancelled; a job calls it between its steps.
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.
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 (*Registry) Answer ¶
Answer answers the question a job is waiting on. It reports false when the job is not waiting.
func (*Registry) Cancel ¶
Cancel stops a job at its next step, answering no to a question it waits on.
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.