Documentation
¶
Overview ¶
Package runner is the only code that starts a process.
Index ¶
- Constants
- Variables
- func BesideExit(err error) []string
- func Followable(f *os.File) error
- func Interrupt(pid int) error
- type Command
- type Detached
- type Ending
- type Exec
- func (x Exec) Detach(ctx context.Context, d Detached) (Started, error)
- func (x Exec) Run(ctx context.Context, cmd Command) (Result, error)
- func (x Exec) Stream(ctx context.Context, cmd Command, out Output) (Ending, error)
- func (x Exec) StreamFile(ctx context.Context, cmd Command, out FileOutput) (Ending, error)
- type ExitError
- type FileOutput
- type Output
- type Result
- type Started
Constants ¶
const Grace = 60 * time.Second
Grace is how long a streamed run has to end after the interrupt its ended context sends, before its process group is killed: long enough for OpenTofu to finish the call it's in and write its state.
Variables ¶
var ErrFollowFailed = errors.New("a run whose output couldn't be followed")
ErrFollowFailed is returned, wrapped, for a run streamed through a file whose output couldn't be followed as it was written, or handed over: what its caller read of it may be short. It sits beside the run's own error, never inside an *ExitError's.
var ErrIncompleteDetach = errors.New("a detached process needs a folder to run in and a file for its stderr")
ErrIncompleteDetach is returned, wrapped, for a Detached with no folder to run in or no file for its stderr.
var ErrNoSuchProcess = errors.New("no process a run's can have")
ErrNoSuchProcess is returned, wrapped, for a pid no process of a run's can have: none above 1, which is launchd's, and where 0 and below would signal a group, or every process.
var ErrNotFound = errors.New("not found on the PATH")
ErrNotFound is returned, wrapped, when the binary isn't on the PATH.
var ErrNotStarted = errors.New("not started")
ErrNotStarted is returned, wrapped, for a run that never started: its context already ended, its binary not found, not one it may run, a folder it can't run in, or arguments past the system's limit. Nothing ran.
var ErrUnfollowable = errors.New("a file a run can't stream through")
ErrUnfollowable is returned, wrapped, for a file a run can't stream through (Followable), and beside ErrNotStarted for a run given one, or one it can't watch: nothing ran.
Functions ¶
func BesideExit ¶
BesideExit are the errors joined in err, a line apiece, each in its own words, but a run's *ExitError where it exited by itself, which what the child said of its failure explains. A signal's end is kept, in its own words, since nothing the child said explains it.
func Followable ¶
Followable is nil where a run can stream through f: a regular file, open to write and to append, that its name still opens. Else it's why not, an ErrUnfollowable.
func Interrupt ¶
Interrupt sends the process pid one interrupt, as a cancel of a run whose own process has gone sends its Terragrunt (decision 52): the process alone, never its group, so OpenTofu hears it only as Terragrunt passes it on. A process already gone is no error: nothing's left to interrupt.
Types ¶
type Command ¶
type Command struct {
// Dir is where it runs; "" for terrace's own directory. Where it's set,
// the run's PWD names it, as a shell starting there would: a tool may
// read its folder from PWD rather than asking the system.
Dir string
// Env is the whole environment the run gets, NAME=value each, and
// nothing else: nil is an empty one. internal/childenv builds it.
Env []string
Name string
Args []string
}
Command is one run of a binary.
type Detached ¶
type Detached struct {
// Command is what runs. Its Dir is needed, so the process never keeps
// terrace's own folder, which may be a worktree removed while it runs.
Command Command
// Stderr is the file the process writes its errors to, so a crash
// leaves its words. It's needed.
Stderr *os.File
// Files are handed to the process after stderr, as fds 3, 4 and on, in
// order.
Files []*os.File
}
Detached is a process to start detached.
type Ending ¶
type Ending struct {
// Signals are those sent to the run as its context ended, in order:
// none, an interrupt, or an interrupt and then a kill.
Signals []syscall.Signal
// Left are those sent to what the process cmd started left of its group
// once it had exited, as a local-exec's child, or OpenTofu where
// Terragrunt died: none where it left nothing, else a terminate, then
// a kill where anything's still there after its grace.
Left []syscall.Signal
}
Ending is how a streamed run ended.
type Exec ¶
type Exec struct{}
Exec runs real processes. Each gets exactly its command's environment, PWD aside, and never the terminal: stdin is empty, both output streams are captured, and it runs in a session of its own, with no controlling terminal, so a child that opens /dev/tty to prompt fails at once rather than stopping until it's killed. The binary is found on terrace's own PATH.
func (Exec) Detach ¶
Detach starts d's process to outlive terrace, as a run's own process does (spec 4.1.5, 4.8.7), and returns once it has started, never waiting for it to end. It runs in a session of its own, so no hangup or signal sent to terrace's terminal or group reaches it, with stdin and stdout on the null device, stderr on d.Stderr, exactly its command's environment, PWD aside, and no fd of terrace's but d.Files: the caller closes its own copies of those once Detach returns. Should it end while terrace still runs, it's reaped, so it leaves no zombie. A start that fails is ErrNotStarted, and nothing ran.
func (Exec) Run ¶
Run runs cmd and returns what it printed. A run that exits non-zero is an *ExitError, with what it printed kept in the Result. It runs as Stream runs it, in a process group of its own, but with interruptGrace for Grace.
func (Exec) Stream ¶
Stream runs cmd in a session and process group of its own, writing its output to out as it comes, through pipes, and returns once the run has ended and its output is written. When ctx ends, the process cmd started gets an interrupt, which it passes on to its own children, as Terragrunt does, and its whole group a kill if it hasn't ended within Grace; the run's error then holds ctx's. Once that process has exited, what it left of its group is ended: killed on a cancelled run, else terminated, then killed if anything's still there after Grace. A run that exits non-zero is an *ExitError, and one that never started ErrNotStarted.
A pipe dies with its reader: should terrace die first, the child dies at its next write, and so does a child of its own writing to it. A run that must outlive terrace streams through a file (StreamFile).
func (Exec) StreamFile ¶
StreamFile runs cmd as Stream does, but for its output, which the child writes to out.File, and which is read as the file grows, from where it ends as StreamFile is called until what's left of the run's group has ended, then to the file's end. A File it can't stream through (Followable), or can't watch, is ErrUnfollowable and ErrNotStarted: nothing ran. Output it couldn't follow or hand over is ErrFollowFailed, beside the run's own error.
type ExitError ¶
type ExitError struct {
// Code is the run's exit status: -1 where a signal ended it.
Code int
Err error
}
ExitError is a run that ended with a status other than 0, or that a signal ended.
type FileOutput ¶
type FileOutput struct {
// File is a regular file, open to write and to append, the child
// writes both its streams to, in the order it writes them. A file,
// unlike a pipe, never loses its reader, so the child runs on whatever
// happens to terrace. The run reads it by its name, which must still
// name it, from where it ends when StreamFile is called, until the
// run's group has ended, then to its end: a process that leaves the
// group yet keeps the file, as a hook's daemon in a session of its own,
// writes outside what's read once the group has ended. The caller
// closes it once the run has ended.
File *os.File
// Out gets what the child writes to File, in the order written, as it's
// written, though up to a few milliseconds behind where a pipe would
// hand it over; nil drops it. It's written from a goroutine of the
// run's own, which nothing else writes it on, alongside OnStart. A
// writer that blocks holds up the run's end, but never the child. Its
// first error stops it being written, and is the run's, an
// ErrFollowFailed.
Out io.Writer
// OnStart and OnInterrupt are as Output's.
OnStart func(Started)
OnInterrupt func()
}
FileOutput is where a streamed run's output goes through a file: the child writes both its streams to File, which the run reads as it grows, handing what it reads to Out.
type Output ¶
type Output struct {
Stdout, Stderr io.Writer
// OnStart, where it's set, gets the run's process ids as soon as it
// has started: what ends the run's group should terrace die before it
// does. It's called on the goroutine Stream runs on, alongside the
// writes of the run's output, and must not block: the run is already
// going, and its interrupt doesn't wait on it.
OnStart func(Started)
// OnInterrupt, where it's set, is called as ctx's end has the run
// interrupted, just before its interrupt is sent: what records that
// it's sent, so that nothing sends it a second (decision 52). It's
// called on a goroutine of the run's own, and the interrupt waits on
// it.
OnInterrupt func()
}
Output is where a streamed run's output goes as the child writes it. One writer given as both takes both streams through one pipe, in the order the child wrote them. Each writer is written from os/exec's own goroutine, which nothing else reads the child's output on: a writer that blocks holds the child up.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package runnertest is a fake runner.Runner, seeded by command line, for the tests of each binary's adapter.
|
Package runnertest is a fake runner.Runner, seeded by command line, for the tests of each binary's adapter. |