Documentation
¶
Overview ¶
Package scriptrun runs the commands of the Scripts tab, a release, a hooks action, a checks run, a script, or a clone's fast-forward pull, and a tool's own updater, in the app or in the user's terminal, and keeps their history.
The Engine starts a run and follows it to its end. A run in the app starts in its own process group and writes straight into its log file, never through a pipe, so a crash of the app can't cut its output short; the engine tails the file every 100 ms and feeds what is new to the run's output reader. A run in the terminal is opened through a TerminalOpener, and followed through the files its script writes: the PID of the shell that runs it, its log, and its exit status.
Every run holds its clone's lock (clonelock) from its start to its end, and a busy clone fails a run at once, naming what holds it: runs never queue on a clone. Up to four runs go in the app at once; a release never waits for a free slot, and any other run does.
It owns the script_runs table: one row per run, written as it starts and as it ends. The newest 200 runs and 512 MB of logs are kept, the oldest finished runs going first, and a finished log over 16 MB keeps only its first 4 MB and its last 12 MB. When the app starts again, the runs it left behind are reconciled: one still going is followed again, and one that is gone is marked interrupted. When the app quits, every run in the app is stopped.
The Service is what the window binds: the history, a run's output in pages, Stop, Run again, and the quit guard's answer. The Engine itself is not bound.
Index ¶
- Constants
- Variables
- func Decode[T any](recorded any) (T, error)
- func DisplayCommand(argv []string, home string) string
- func KeepNote(list []string, line string) []string
- func OpenFolder(ctx context.Context, opener FolderOpener, dir string) error
- func ScriptName(id int64) string
- type Amendment
- type Callbacks
- type Chunk
- type Config
- type Ending
- type Engine
- func (e *Engine) Active() bool
- func (e *Engine) Amend(ctx context.Context, id int64, change func(Record) (Amendment, bool)) (bool, error)
- func (e *Engine) ClearAgentHistory(ctx context.Context) error
- func (e *Engine) Ended(ctx context.Context, kind Kind) ([]Record, error)
- func (e *Engine) InUse(tool string) bool
- func (e *Engine) Kept(id int64, into any) (bool, error)
- func (e *Engine) LetFinish(on bool)
- func (e *Engine) Live(kind Kind) []LiveRun
- func (e *Engine) LogKept(ctx context.Context, id int64) bool
- func (e *Engine) RegisterRerunner(kind Kind, r Rerunner)
- func (e *Engine) RegisterVerifier(kind Kind, v Verifier)
- func (e *Engine) Run(ctx context.Context)
- func (e *Engine) Start(ctx context.Context, spec Spec) (int64, error)
- func (e *Engine) StopAll()
- func (e *Engine) StopKind(ctx context.Context, kind Kind) (int, error)
- func (e *Engine) Updating(tool string) int64
- type Failure
- type Filter
- type FolderOpener
- type Kind
- type LineWriter
- type LiveRun
- type Notes
- type Outcome
- type Progress
- type Record
- type Rerunner
- type Result
- type Revealer
- type Run
- type RunsIn
- type Service
- func (s *Service) ClearHistory(ctx context.Context) error
- func (s *Service) KeepRunning(_ context.Context)
- func (s *Service) LetThemFinish(_ context.Context)
- func (s *Service) MarkSeen(ctx context.Context, id int64) error
- func (s *Service) OpenInTerminal(ctx context.Context, id int64) error
- func (s *Service) Output(ctx context.Context, id, offset, limit int64) (Chunk, error)
- func (s *Service) QuitAnyway(_ context.Context)
- func (s *Service) Run(ctx context.Context, id int64) (Run, error)
- func (s *Service) RunAgain(ctx context.Context, id int64) (int64, error)
- func (s *Service) Runs(ctx context.Context, filter Filter) ([]Run, error)
- func (s *Service) ShowLog(ctx context.Context, id int64) error
- func (s *Service) Stop(ctx context.Context, id int64) error
- type Spec
- type Status
- type TerminalOpener
- type TerminalRun
- type Verification
- type Verifier
Constants ¶
const ( // Slots is how many runs go in the app at once, across every clone. Slots = 4 // KeepRuns is how many runs the history keeps, and KeepLogBytes how much log in all; // the oldest finished runs go first, and a run still going is never dropped. KeepRuns = 200 KeepLogBytes = 512 << 20 // KeepAgentRuns and KeepAgentLogBytes are the same for the agents' runs, which are // counted apart, so they never push the Scripts tab's runs out, and kept no longer // than Config.AgentLogsFor says. KeepAgentRuns = 200 KeepAgentLogBytes = 1 << 30 // DefaultAgentLogsFor is how long an agent's run is kept without Config.AgentLogsFor. DefaultAgentLogsFor = 30 * 24 * time.Hour // A finished log over CompactOver bytes keeps its first CompactHead bytes and its last // CompactTail. CompactOver = 16 << 20 CompactHead = 4 << 20 CompactTail = 12 << 20 // QuitKillAfter is how long a run in the app has, after SIGTERM as the app quits, // before it gets SIGKILL. It fits inside the desktop shell's stop timeout. QuitKillAfter = 3 * time.Second )
const ( // RunsInApp is a run the app starts and follows itself. RunsInApp = integrationprefs.RunsInApp // RunsInTerminal is a run in the user's terminal app, which the app follows. RunsInTerminal = integrationprefs.RunsInTerminal )
The places a run runs.
const FolderScript = "run-0"
FolderScript names the script that opens the terminal in a clone and runs nothing there, for a page's Open in terminal: no run owns it, so it takes run 0's name, which the launch folder's pruning knows (see ScriptName).
const MaxNotes = 200
MaxNotes caps each list of lines a reader of a run's output keeps, its warnings among them: the first ones.
const MaxUnrecognized = 200
MaxUnrecognized caps the lines a run's result keeps that its reader didn't recognize; it still counts them all.
Variables ¶
var ( // ErrInvalidSpec is returned for a run missing what starting it needs. ErrInvalidSpec = errors.New("invalid run") // ErrNotFound is returned for a run the history no longer holds. ErrNotFound = errors.New("the run is no longer recorded") // ErrNoSlot is returned for a release when every slot for a run in the app is busy. ErrNoSlot = errors.New("every run slot is busy") // ErrNotRunning is returned when stopping a run that has ended. ErrNotRunning = errors.New("the run has ended") // ErrStillGoing is returned when amending a run that hasn't ended. ErrStillGoing = errors.New("the run is still going") // ErrNoStop is returned when stopping a run in the terminal, which stops there. ErrNoStop = errors.New("a run in the terminal can't be stopped from the app") // ErrStopRefused is returned when stopping a run whose kind says it can't be stopped // now (Progress.StopRefused), as a release while magex pulls. ErrStopRefused = errors.New("the run can't be stopped now") // ErrNoRerun is returned for Run again on a release, or on a kind with no Rerunner. ErrNoRerun = errors.New("the run can't run again") // ErrNoTerminal is returned for a run in the terminal where none can be opened. ErrNoTerminal = errors.New("runs in the terminal aren't available") // ErrClosed is returned once the engine has stopped every run, as the app quits. ErrClosed = errors.New("the runner has stopped") // ErrNoFolder is returned for Open in terminal on a run whose folder is gone. ErrNoFolder = errors.New("the run's folder is gone") // ErrNoLog is returned for Show the log file on a run whose log is gone. ErrNoLog = errors.New("the run's log is gone") // ErrNoReveal is returned where no file manager can show a file. ErrNoReveal = errors.New("files can't be shown here") )
var ErrNoFolderTerminal = errors.New("the terminal can't be opened here")
ErrNoFolderTerminal is returned by OpenFolder where the user's terminal can't be opened.
Functions ¶
func Decode ¶
Decode reads back, as a T, what a run recorded as its intent or its kind's own result (Result.Intent, Result.Outcome): the history decodes them as plain JSON values, which Decode encodes again and decodes into a T. It returns the zero T, with json's error, when they can't be read as one.
func DisplayCommand ¶
DisplayCommand writes argv out as a shell would read it, each word as textutil.ShellWord quotes it, with the program's folder under home written from ~, as the window shows every folder. The ~/ stays outside the quotes, where a shell still reads it as home. The program is a path that starts with a slash, as every run's is (validArgv) and a tool's copy is, so ShellWord's rule lets it stand plain. A run's Command defaults to it, and the integrations show a tool's own update command with it.
func OpenFolder ¶
func OpenFolder(ctx context.Context, opener FolderOpener, dir string) error
OpenFolder opens the user's terminal in the clone at dir through opener, as FolderScript, for a page's Open in terminal: the user looks at the clone or fixes it there, and the app runs nothing in it. A nil opener, where no terminal can be opened, is ErrNoFolderTerminal, in words the window shows.
func ScriptName ¶
ScriptName names the script that opens run id in the terminal, or the terminal in its folder: run-<id>, a prefix the launch folder's pruning knows, so the scripts are removed once they are old.
Types ¶
type Amendment ¶
Amendment is what Amend writes over the record of a run that has ended: the sentence that says how it came out, and its kind's own result (Result.Outcome), which must encode as JSON. The rest of the record stays as it is.
type Callbacks ¶
type Callbacks struct {
// Output is the run's output reader: it is fed every byte of the log in order, as it
// arrives, from one goroutine at a time. nil reads nothing.
Output io.Writer
// Progress says how far the run has got, and whether Stop is let through now, as its
// kind works it out from what Output has read. The engine asks it as the window reads
// the run, at each Stop, and after each read of the log, telling Config.Progressed when
// the answer changed. It is called from any goroutine, must return promptly, and must
// never call back into the engine. nil has no steps, and lets Stop through.
Progress func() Progress
// Finish works out the run's outcome once it has ended and Output has read all of
// it. It is given how the run ended and the outcome the engine worked out from that,
// and returns the one to record. nil records the engine's. Its context ends when the
// app quits and can wait no longer, and Finish must then return at once: with an
// Unsettled outcome, when what it needed to know couldn't be learned in time.
Finish func(ctx context.Context, end Ending, out Outcome) Outcome
// Recorded is called once the run's end is recorded, with the outcome recorded, before
// the run lets go of its clone: what is told of an outcome is told only once it is
// kept, so a record that failed, which the next start settles again, tells nothing
// twice. It is never called for an outcome left Unsettled, nor when the record failed.
// It runs on the goroutine that recorded the run, and must return promptly. nil tells
// nothing.
Recorded func(ctx context.Context, id int64, out Outcome)
// Released is called once the run has let go of its clone and its slot, after its end
// was recorded, or its record failed: what it tells sees the clone free and the run
// ended, as a page that reads the clone again after a run must. It isn't called for a run
// left going as the app quit. It runs on the goroutine that recorded the run, must return
// promptly, and must never call back into the engine. nil tells nothing.
Released func(id int64)
// Over reports that the run's output says it is done, though its program may still
// run: an agent's result line, after which the agent may wait on a shell it started in
// the background. The engine asks it after each read of the log, and once it reports
// true, stops the run's group as Stop does, without counting the run as stopped. It
// has Progress's rules. nil leaves the program to end by itself.
Over func() bool
}
Callbacks are what a run's kind hears of the run and tells of it, as it goes and as it ends. A Spec gives a run them as it starts, and its kind's Verifier gives them again to a run from before the app restarted (Verification): each kind builds both from one helper, so a run picked up after a restart is read, judged, and told of as one it started. Any may be nil.
type Chunk ¶
type Chunk struct {
// Text is the part, cut between characters.
Text string `json:"text"`
// Offset is where in the log Text starts, and Next where the next part starts: the
// offset to ask for next.
Offset int64 `json:"offset"`
Next int64 `json:"next"`
// Size is the log's size now.
Size int64 `json:"size"`
// Done reports that the run has ended and Next is the log's end: no more will come.
Done bool `json:"done"`
// Restart reports an offset past the log's end, as when a long log was cut down at its
// run's end: read it again from the start.
Restart bool `json:"restart"`
}
Chunk is a part of a run's log, for the window.
type Config ¶
type Config struct {
// DB holds script_runs.
DB *sql.DB
// Locks is the app's one set of clone locks; nil gives the engine a set of its own.
Locks *clonelock.Set
// LogDir is the absolute folder the runs' logs go in: <data>/script-runs.
LogDir string
// Home is the user's home folder, for ~ in the folders the window shows and in a run's
// command, which records its program's absolute path all the same.
Home string
// Clock stamps the runs. It defaults to the system clock.
Clock clock.Clock
// TimeLimit makes the timer that stops a run the engine started in the app at its
// time limit: what fires once d has passed, and what stops it early. nil is the
// system clock's timer; a test holds the limit and fires it itself, so no test waits
// out a real one.
TimeLimit func(d time.Duration) (fire <-chan time.Time, stop func() bool)
// Logger gets one record per run that ends.
Logger *slog.Logger
// Changed is called whenever a run starts, ends, or changes status, or the history
// changes, never for output. Core emits event.ScriptRunsUpdated from it. It must
// return promptly and never call back into the engine.
Changed func()
// Progressed is called with a run's ID whenever its Progress changes: it reached a
// step, or Stop turned on or off. Nothing else about the runs changed, so core emits
// event.ScriptRunProgress from it, never ScriptRunsUpdated. It has Changed's rules, and
// may be nil.
Progressed func(id int64)
// Failed is told of each run whose end is recorded as failed, one stopped at its time
// limit included, once that end is kept: every kind, in the app or in the terminal,
// started now or settled after a restart, as its kind's Verifier works it out or as the
// engine reads how it ended. A run the user stopped, one the quit stopped or a restart
// interrupted, one that succeeded, and one left Unsettled tell nothing, and no run tells
// twice: only the record that marks it ended tells, and a record that fails tells nothing
// until the next start settles the run. Core tells the user (notify's run_failed). It
// runs on the goroutine that recorded the run, after its Spec's Recorded, must return
// promptly, and must never call back into the engine. nil tells nothing.
Failed func(ctx context.Context, f Failure)
// Terminal opens runs in the user's terminal, and the terminal in a run's folder.
// Without it, runs there fail to start, and Open in terminal fails.
Terminal TerminalOpener
// Reveal shows a run's log file in Finder. Without it, Show the log file fails.
Reveal Revealer
// Quit is what the Service's QuitAnyway calls: the user chose to stop the runs and
// quit. Kept is what its KeepRunning calls: the user chose to keep the app running.
Quit func()
Kept func()
// Folders is the absolute folder the runs' own folders (Spec.Folder) are made in:
// <data>/agent-runs. A run's folder must be directly inside it; "" allows none.
Folders string
// AgentLogsFor says how long an agent's run that ended is kept (Autopilot's run logs
// setting); nil, or a duration that isn't positive, keeps DefaultAgentLogsFor.
AgentLogsFor func(ctx context.Context) time.Duration
}
Config holds an Engine's dependencies. DB and LogDir are required.
type Ending ¶
type Ending struct {
// ExitCode is the program's exit status, or nil when it is unknown.
ExitCode *int
// Stopped reports that the user stopped it, TimedOut that it ran past its limit and
// was stopped, and Quit that the app stopped it as it quit.
Stopped bool
TimedOut bool
Quit bool
// Killed reports that the run's group was sent SIGKILL, which leaves alive whatever of
// it left the group, as an agent's background shell does.
Killed bool
// Interrupted, when set, says why its end couldn't be seen: "The terminal closed while
// this was running."
Interrupted string
}
Ending is how a run ended, as the engine saw it.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine starts runs and follows them to their end. It is safe for concurrent use. Run is its desktop worker, and the Service its bound face; the Engine itself is not bound, since a Spec holds functions.
func (*Engine) Active ¶
Active reports whether a run is going in the app, one from before the app restarted included: quitting would stop it. A run in the terminal doesn't count, nor does one whose end is recorded. It never waits on anything but the engine's own lock, so the quit guard can ask it on the main thread.
func (*Engine) Amend ¶
func (e *Engine) Amend(ctx context.Context, id int64, change func(Record) (Amendment, bool)) (bool, error)
Amend rewrites what the record of a run that has ended says about how it came out, as a release does while it follows its workflow on GitHub after magex ended. change is given the run as recorded now, inside the transaction that writes it, and returns what to write, or false to leave the run as it is. Amend reports whether it changed the run, and the window hears of a change (Config.Changed). A run the history no longer holds fails with ErrNotFound, and one still going with ErrStillGoing: its end, once recorded, would write over the amendment.
func (*Engine) ClearAgentHistory ¶ added in v0.4.0
ClearAgentHistory forgets every agent's run that has ended, with its log and its folder, as Autopilot's Clear history does; the runs going are kept, and so is every other kind's.
func (*Engine) Ended ¶
Ended returns the runs of kind that have ended, oldest first, as the history holds them, reading only those: a release's follow of its workflow is resumed from them when the app starts again.
func (*Engine) InUse ¶
InUse reports whether a run going now uses tool, an integration's ID such as "mage-x". The installer waits for such a run to end before it replaces the tool. The tool's own update doesn't count: it is the replacement, not a run the replacement would cut across. Nor does a run whose end is recorded, which the engine is only letting go of.
func (*Engine) Kept ¶
Kept reads what the run id keeps beside its record (Spec.Keep) into into, and reports whether it keeps anything: nothing once its end is recorded, nor for a run whose kind keeps nothing, or whose state couldn't be written. A kind's Verifier reads it to settle a run from before the app restarted as its Finish would have.
func (*Engine) LetFinish ¶ added in v0.4.0
LetFinish says whether the runs that may outlive a quit (Spec.OutlivesQuit) are let go as the app quits rather than stopped: the user's "Let them finish". StopAll reads it.
func (*Engine) Live ¶ added in v0.4.0
Live returns the runs of kind going now, in the app or from before the app restarted, those still starting included: every folder an agent works in, though the process list leaves the app's own children and an orphan out. A run whose end is recorded is no longer going.
func (*Engine) LogKept ¶ added in v0.4.0
LogKept reports whether the history still holds run id's log: false once the retention or Clear history forgot the run.
func (*Engine) RegisterRerunner ¶
RegisterRerunner makes r work out Run again for runs of kind. A release takes none: Run again is never offered for one.
func (*Engine) RegisterVerifier ¶
RegisterVerifier makes v verify the runs of kind from before the app restarted: it gives each again the Callbacks its Spec gave it. Register it before Run, which picks those runs up.
func (*Engine) Run ¶
Run is the engine's desktop worker. It reconciles the runs the app left behind when it last quit or crashed, then waits for ctx to end and stops every run in the app with StopAll. Each live run's log is tailed by the goroutine that follows it.
func (*Engine) Start ¶
Start starts the run spec describes and returns its ID. In order, it takes the clone's lock, failing at once with a *clonelock.BusyError when another job holds it; takes a slot, for a run in the app, waiting for one until ctx ends unless it is a release, which fails at once with ErrNoSlot; runs spec.Before; records the run; and starts it, in the app or in the terminal. A run that was recorded but couldn't start is recorded as failed, and its ID comes back with the error.
func (*Engine) StopAll ¶
func (e *Engine) StopAll()
StopAll stops every run in the app, as the app quits: SIGTERM to each as soon as its kind lets it be stopped, which is at once unless it refuses (a release while magex switches branches or pulls), SIGKILL to those left QuitKillAfter after the quit began, and each run's record then says it was stopped. A run whose kind still refuses QuitKillAfter in is never signaled: it is let go, still recorded as running, and the next start follows it again or settles how it ended, as it does a run in the terminal, which is always left alone. Nothing starts after it. It returns once the runs are recorded, or given up on.
func (*Engine) StopKind ¶ added in v0.4.0
StopKind stops every run of kind going in the app, from before the app restarted too, as Stop does each, and returns how many it stopped: Stop all for the agents' runs. Unlike StopAll, the quit path, it leaves the engine open, so a run can start after it, and it leaves every other kind's runs alone. A run whose kind refuses to stop now is left going.
type Failure ¶
type Failure struct {
// ID is the run's, which a click on the notification opens, and Started when the run
// started, as its record holds it, which tells it from a later run given its ID once the
// history was cleared.
ID int64
Started time.Time
// Repo is the clone's repository on GitHub, or the zero Name for a folder that isn't
// one, which Title then names instead.
Repo repo.Name
// Title names what failed and the run's own title, as the Runs page lists it, and the
// run's folder when it has no repository: "Checks failed: Checks · all files", "A
// tool's update failed in ~/go/bin: Update magex". Body says how it ended: its summary
// ("It exited with status 1."), or, for a run stopped at its time limit, "Timed out
// after 30 minutes."
Title string
Body string
}
Failure is a run whose end is recorded as failed, as Config.Failed is told of it: what the user is told, a notification in the log and its banner (run_failed in core), and the run a click on it opens.
type Filter ¶
type Filter struct {
// Kinds are the kinds listed; none lists every kind but an agent's (WithAgents).
Kinds []Kind `json:"kinds"`
// Repo is part of a repository's "owner/name" to look for, in any case; "" lists
// every run.
Repo string `json:"repo"`
// WithAgents lists the agents' runs too, with no Kinds: the Runs page and the Scripts
// tab's count leave them out, as the Autopilot tab is their home, while the quit and an
// update's restart, which would stop them, name them.
WithAgents bool `json:"withAgents"`
}
Filter picks the runs Runs lists.
type FolderOpener ¶
FolderOpener opens the user's terminal in a folder, running nothing there, under the script name it is given. Core adapts the terminal the runs open in.
type Kind ¶
type Kind string
Kind is what a run does.
const ( // KindRelease is a release: magex tags the newest commit and pushes the tag. KindRelease Kind = "release" // KindHooks installs, updates, or removes a repository's go-pre-commit hooks. KindHooks Kind = "hooks" // KindChecks runs go-pre-commit's checks. KindChecks Kind = "checks" // KindScript is a script: the user's own, a repository's task, or a magex target. KindScript Kind = "script" // KindUpdate is a tool updating itself: its own updater, such as magex update:install, // replaces the copy it runs as with its latest release. It runs in the copy's folder, // never a clone, and doesn't count as using the tool (InUse): it is the update. KindUpdate Kind = "update" // KindPull fast-forwards a clone's branch to origin's, on the user's click on Repo // scripts: git's fast-forward-only pull, and nothing else. KindPull Kind = "gitPull" // KindAgent is a coding agent's run in the background, which Autopilot starts in an // item's worktree: it holds the worktree, never the clone, so two items of one clone // run side by side. Its history is kept apart from the others' (KeepAgentRuns), it // tells Config.Failed nothing (Autopilot tells how it ended), and the Runs page leaves // it out (Filter.WithAgents): the Autopilot tab is its home. KindAgent Kind = "agent" )
The kinds of run.
func (Kind) Timeout ¶
Timeout returns how long a run of the kind may take before it is stopped, or 0 for no limit: five minutes for a release, one for a hooks action, thirty for checks, ten for a tool updating itself (its download and swap, which the tools bound at six), thirty for a pull (its fetch, then its checkout, Git LFS's downloads included, which nothing should cut off), thirty for an agent, whose fence names its own, and none for a script, which the user stops.
type LineWriter ¶
type LineWriter struct {
// contains filtered or unexported fields
}
LineWriter is the part every kind's reader of a run's output shares: an io.Writer for the run's Spec.Output, fed the log's bytes in order and split anywhere, which hands each whole line, as textutil.LineSplitter cleans it, to the func the reader gave NewLineWriter. It does so under its lock, which the reader's own methods take too (Lock and Unlock) to read what that func kept.
func NewLineWriter ¶
func NewLineWriter(read func(line string)) *LineWriter
NewLineWriter returns a writer that hands each line it reads to read.
func (*LineWriter) Flush ¶
func (w *LineWriter) Flush()
Flush reads the last line when the output ended without a newline. Call it once the output has ended.
func (*LineWriter) Lock ¶
func (w *LineWriter) Lock()
Lock takes the lock no line is read without, so what the reader kept can be read.
type LiveRun ¶ added in v0.4.0
type LiveRun struct {
ID int64
Kind Kind
Title string
// Dir is the folder it runs in, and PID its program's process, or 0 while it starts.
Dir string
PID int
// Orphaned reports a run from before the app restarted.
Orphaned bool
}
LiveRun is a run going now, as the folders it works in are counted: one the engine started, or one from before the app restarted that it follows again.
type Notes ¶
type Notes struct {
Warnings []string `json:"warnings"`
Unrecognized []string `json:"unrecognized"`
UnrecognizedCount int `json:"unrecognizedCount"`
}
Notes are the lines a reader of a run's output keeps for the run's result: the tool's warnings, the first MaxNotes, and the lines the reader didn't recognize, of which it keeps the first MaxUnrecognized and counts them all.
type Outcome ¶
type Outcome struct {
// Status is how it ended, and Summary says so in a sentence.
Status Status
Summary string
// Warnings are the tool's warning lines.
Warnings []string
// Unrecognized are the lines the reader didn't recognize, of which the first
// MaxUnrecognized are kept, and UnrecognizedCount counts them all.
Unrecognized []string
UnrecognizedCount int
// Detail is the kind's own result, encoded as JSON.
Detail any
// Unsettled, from a Finish whose context ended first, records nothing: the run stays
// recorded as going, and the next start of the app settles it again, through its
// kind's Verifier. Every other field is then ignored.
Unsettled bool
}
Outcome is what a run's record says about how it ended.
type Progress ¶
type Progress struct {
// Step is the last step the run reached, and Working the one under way, in its kind's
// own names, as a release's "onBranch" or "tagCreated"; "" for none yet, or for a kind
// without steps.
Step string `json:"step"`
Working string `json:"working"`
// StopRefused, when set, says why Stop is off now, as "Stopping now could leave a
// rebase in progress.": Stop refuses with it.
StopRefused string `json:"stopRefused"`
// StopConfirm, when set, is what the window asks before it stops the run, since
// stopping now leaves something behind: "Stopping now can leave v1.4.3 on this Mac but
// not on GitHub, and your clone on master."
StopConfirm string `json:"stopConfirm"`
}
Progress is how far a run going now has got, as its kind reads its output, and whether the user may stop it now.
type Record ¶
type Record struct {
ID int64
Kind Kind
Title string
Source string
Repo repo.Name
Dir string
Argv []string
Command string
RunsIn RunsIn
Status Status
// ExitCode is the program's exit status, or nil when unknown or still running.
ExitCode *int
// PID and PGID are its process and process group once known, or 0, and
// ProcStartedAt is when that process started, or zero.
PID int
PGID int
ProcStartedAt time.Time
Summary string
Result Result
LogBytes int64
// SeenAt is when the user saw it, StartedAt when it started, and FinishedAt when it
// ended; SeenAt and FinishedAt are zero until then.
SeenAt time.Time
StartedAt time.Time
FinishedAt time.Time
}
Record is one run as the history holds it.
type Rerunner ¶
Rerunner works a run's command out again from its source, for Run again: the script's current text, the task file as it is now, the same hooks action, or the same magex target. It never replays the recorded argv.
type Result ¶
type Result struct {
// Tool is the integration it used, or "".
Tool string `json:"tool"`
// Intent is what it set out to do, as its kind wrote it at the start, or nil.
Intent any `json:"intent"`
// Warnings, Unrecognized, and UnrecognizedCount are the Outcome's.
Warnings []string `json:"warnings"`
Unrecognized []string `json:"unrecognized"`
UnrecognizedCount int `json:"unrecognizedCount"`
// Outcome is the kind's own result, or nil.
Outcome any `json:"outcome"`
// Folder is the run's own folder (Spec.Folder), or "".
Folder string `json:"folder,omitempty"`
}
Result is a run's result, as recorded.
type Run ¶
type Run struct {
ID int64 `json:"id"`
Kind Kind `json:"kind"`
Title string `json:"title"`
Source string `json:"source"`
// Repo is the clone's repository, "owner/name", or "" for a folder that isn't one,
// and Dir the folder, with ~ for home.
Repo string `json:"repo"`
Dir string `json:"dir"`
// Argv is the command as it started, and Command its display form.
Argv []string `json:"argv"`
Command string `json:"command"`
RunsIn RunsIn `json:"runsIn"`
Status Status `json:"status"`
// ExitCode is the program's exit status, or null when unknown or still running.
ExitCode *int `json:"exitCode"`
// Summary says how it ended, or for an orphaned run, that it runs on.
Summary string `json:"summary"`
Result Result `json:"result"`
// LogBytes is its log's size: so far, while it runs.
LogBytes int64 `json:"logBytes"`
// Seen reports that the user has seen it, which clears the tab's red dot.
Seen bool `json:"seen"`
// StartedAt is when it started, and FinishedAt when it ended, or "", as RFC 3339.
StartedAt string `json:"startedAt"`
FinishedAt string `json:"finishedAt"`
// Progress is how far a run going now has got, as its kind reads its output, and
// whether Stop is off or asks first; zero once it has ended. Its stop texts are ""
// for a run in the terminal, which stops there.
Progress Progress `json:"progress"`
// CanStop reports a run in the app going now that its kind lets stop now
// (Progress.StopRefused says why not); CanRunAgain a run whose command can be worked
// out again, never a release.
CanStop bool `json:"canStop"`
CanRunAgain bool `json:"canRunAgain"`
// OutlivesQuit reports a run going in the app that may go on once the app has quit, as
// an agent that keeps to a budget of its own does (Spec.OutlivesQuit): the quit's Let
// them finish lets it go, and the next start follows it again.
OutlivesQuit bool `json:"outlivesQuit"`
}
Run is one run as the window shows it. Every field is a plain value.
type RunsIn ¶
type RunsIn = integrationprefs.RunsIn
RunsIn is where a run runs: the setting's own type, so the user's choice goes straight into a Spec.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the runs as the window sees them: the history, a run's output, Stop, Run again, and the quit guard's answer. The window binds it; it starts nothing new itself but Run again.
func (*Service) ClearHistory ¶
ClearHistory forgets every run that has ended, with its log, but the agents' runs, which the Runs page doesn't list: Autopilot's Clear history forgets those. Runs going are kept.
func (*Service) KeepRunning ¶ added in v0.3.1
KeepRunning is the user's other answer to quitting while runs go in the app: keep it running. The runs go on, and the desktop shell forgets what the quit was to do after, such as opening the copy a move to Applications made.
func (*Service) LetThemFinish ¶ added in v0.4.0
LetThemFinish is the user's third answer to quitting while runs go in the app, offered while every agent's run going can outlive the quit (Run.OutlivesQuit): quit, and let those runs finish on their own. Shutdown then lets them go, still recorded as going, and the next start follows each again; it stops every other run, as Stop and quit does.
func (*Service) OpenInTerminal ¶
OpenInTerminal opens the terminal chosen for launches in a run's folder, the clone, so the user can look at it or finish by hand what the run left; the app runs nothing there.
func (*Service) Output ¶
Output returns up to limit bytes of a run's log from offset, cut between characters; a limit of 0 or more than 256 KB reads 256 KB. A negative offset reads the log's last limit bytes. A log not written yet reads as empty.
func (*Service) QuitAnyway ¶
QuitAnyway is the user's answer to quitting while runs go in the app: stop them and quit. The desktop shell then lets the quit through, and shutdown stops the runs.
func (*Service) RunAgain ¶
RunAgain starts a run again, its command worked out again from its source by its kind's Rerunner, and returns the new run's ID. A release is refused: it runs again from its sheet.
func (*Service) Stop ¶
Stop starts stopping a run in the app: SIGINT to its process group, SIGTERM five seconds later, and SIGKILL ten after that. It returns at once; the run's record says it stopped once it has. A run in the terminal stops there, a run whose kind refuses to stop now (Progress.StopRefused) is refused with its kind's words, ErrStopRefused, and a run whose end is recorded is refused with ErrNotRunning.
type Spec ¶
type Spec struct {
// Callbacks are what its kind hears of the run and tells of it: its output reader,
// progress, Finish, Recorded, and Released.
Callbacks
// Kind is what the run does.
Kind Kind
// Title is what the window calls it: "Release v1.4.3", "Checks · all files".
Title string
// Source says where its command came from, as its kind names it: a script's ID, a
// task file, a magex target. It may be "".
Source string
// Repo is the clone's repository on GitHub, or the zero Name for a folder that isn't
// one.
Repo repo.Name
// Dir is the clone's absolute folder: the run runs there, and holds its lock. A tool
// updating itself runs in, and holds, its copy's folder.
Dir string
// Argv is the command, its program's absolute path first. A shell command runs as
// [shell, -c, command], exactly as written: the engine never adds a shell. It may be
// empty when Before works it out.
Argv []string
// Env is the environment. For a run in the app it is the whole environment, as in
// exec.Cmd.Env, nil being the app's own. For a run in the terminal it is only what the
// script exports, such as a tool's quiet settings, never the login shell's environment,
// which the terminal's own shell sets up.
Env []string
// Command is the display form of Argv; "" writes Argv out, quoted as a shell would, with
// its program's folder under home from ~. Argv itself stays absolute.
Command string
// RunsIn is where it runs; "" is the app.
RunsIn RunsIn
// Timeout is how long it may run before it is stopped: 0 is the kind's Timeout, and a
// negative one is no limit. A run in the terminal has none.
Timeout time.Duration
// Tool is the integration it uses, such as "mage-x", which InUse reports, or "". A
// tool updating itself names the tool it updates, which InUse leaves out.
Tool string
// Intent is what it sets out to do, written into its result as it starts, so a run
// cut short can still be checked. It must encode as JSON; nil is none.
Intent any
// Keep is what the run keeps beside its record while it goes, for its kind's Verifier
// after a restart, such as what Before read of the clone. It is encoded as JSON once
// Before has run, into a file of the run's own beside its log, which Engine.Kept reads,
// never into its record, so the window never gets it, and removed once the run's end is
// recorded, or with its log. A state that can't be written is only logged. nil keeps
// nothing.
Keep any
// Before runs under the clone's lock and a slot, before anything is recorded or
// started; an error ends the start there. It may return the command to run, worked out
// under the lock, which replaces Argv; nil keeps Argv. A release gathers its facts again
// in it and decides there what magex runs.
Before func(ctx context.Context) ([]string, error)
// BeforeEnv, when set, is asked once Before has run without an error, still under the
// clone's lock, for the environment that replaces Env: a kind whose Before works out
// what its command runs with, as a pull works out git's guards there, hands it over
// this way. nil keeps Env.
BeforeEnv func() []string
// Stdin, when set, is the absolute path of a file the program reads on its standard
// input, opened as the run starts, once Before has run: an agent's prompt, which so
// never appears on a command line. "" reads /dev/null. A run in the terminal has none.
Stdin string
// Folder, when set, is a folder of the run's own, which goes with its log: removed when
// the history forgets the run. It must be directly inside Config.Folders, and is
// recorded in its result, so the run's files are found after a restart too. An agent's
// run keeps its prompt and settings there.
Folder string
// OutlivesQuit reports a run that may go on once the app has quit, as an agent that
// keeps to a budget of its own does: when the user lets such runs finish
// (Engine.LetFinish), StopAll lets it go, still recorded as going, and the next start
// follows it again.
OutlivesQuit bool
}
Spec is a run to start.
type Status ¶
type Status string
Status is where a run is.
const ( // StatusRunning is a run going now. StatusRunning Status = "running" // StatusSucceeded is a run that ended well: its program exited 0, or its kind // verified it. StatusSucceeded Status = "succeeded" // StatusFailed is a run that ended badly, couldn't start, or ran past its limit. StatusFailed Status = "failed" // StatusStopped is a run the user stopped, or that the app stopped as it quit. StatusStopped Status = "stopped" // StatusInterrupted is a run that ended while the app wasn't watching: the app quit // or crashed with it inside, or its terminal closed. StatusInterrupted Status = "interrupted" // StatusOrphaned is a run the app started before it restarted, still going. The app // follows it again, and it can be stopped. StatusOrphaned Status = "orphaned" )
The statuses of a run.
type TerminalOpener ¶
type TerminalOpener interface {
OpenRun(ctx context.Context, run TerminalRun) error
FolderOpener
}
TerminalOpener opens a run in the user's terminal app, or the terminal in a run's folder with nothing run there, for Open in terminal. Core adapts the launcher's terminal to it; where no terminal can be opened, it returns an error.
type TerminalRun ¶
type TerminalRun struct {
// Name names the script, "run-<id>" (ScriptName).
Name string
// Dir is the folder it runs in, Argv the command, and Env what the script exports.
Dir string
Argv []string
Env []string
// Log, PIDFile, and ExitFile are the files the engine follows it through.
Log string
PIDFile string
ExitFile string
}
TerminalRun is a run for the terminal to open: a script that changes into Dir, records its shell's PID in PIDFile, runs Argv with its output recorded in Log, and writes its exit status to ExitFile, under a temporary name first and then renamed, so it is never read half written.
type Verification ¶
type Verification struct {
Callbacks
// Timeout is the run's time limit, from its start, which still holds while it is
// followed again: one found past it is stopped at once. 0 sets none.
Timeout time.Duration
// OutlivesQuit is the Spec's: the run may be let go again as the app quits.
OutlivesQuit bool
}
Verification is what a Verifier gives a run from before the app restarted: the Callbacks a Spec gives a run it starts. Any may be nil, as in a Spec.
type Verifier ¶
type Verifier interface {
Verify(run Record) Verification
}
Verifier stands in, for the runs of its kind from before the app restarted, for what their Spec gave them: their Callbacks, closures a restart loses. As the engine picks such a run up, orphaned or interrupted, it asks Verify for them again, built from the run's record alone, its intent above all. It then feeds the reader the run's whole log, from its start, and calls Finish when the run ends, as for a run it started: a release is verified that way, wherever its tag and its clone were left.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
Package scriptruntest is the runner the suites of scriptrun's kinds share (hooks, scripts, and release): a real scriptrun Engine and its Service over a fresh database, the app's next start over the same history, a fake terminal app that runs each run meant for the user's terminal under /bin/sh (Terminal), and waits for a run's end that look again only when the engine says the runs changed, never on a timer (AwaitShell alone polls, for a shell the engine notes without telling).
|
Package scriptruntest is the runner the suites of scriptrun's kinds share (hooks, scripts, and release): a real scriptrun Engine and its Service over a fresh database, the app's next start over the same history, a fake terminal app that runs each run meant for the user's terminal under /bin/sh (Terminal), and waits for a run's end that look again only when the engine says the runs changed, never on a timer (AwaitShell alone polls, for a shell the engine notes without telling). |