Documentation
¶
Overview ¶
Package detach runs a command that must outlive the process that started it: `workspace://…/exec WITH detach = true`.
A job is a directory. The starter writes the job's spec there and launches a WRAPPER — the txco binary in its hidden `__job` mode — in a session of its own. The wrapper runs the command, enforces the job's deadline itself, and writes the exit to disk atomically. Nothing ties the wrapper to its starter: no pipe, no terminal, no process group. So the starter may be killed and restarted, and a later process still reads the job's real exit from its directory.
<root>/<job id>/ spec.json what to run (mode 0600: it holds the environment) stdin the command's stdin, when it has one lock held (flock) by the wrapper for as long as it lives started.json written once the command has started stdout the command's stdout, capped stderr the command's stderr, capped stop created to ask the wrapper to end the command exit.json the outcome, written once, by rename wrapper.log the wrapper's own complaints, normally empty
Three rules carry the design:
- A wrapper is alive exactly while it holds the lock. Liveness is read by trying the lock, never from a pid, so a reused pid is never mistaken for a job.
- exit.json is written before the lock is released. A job with no exit and a free lock is LOST (the wrapper was killed, or the machine restarted): whether its command finished cannot be known.
- A job is stopped by a file, not a signal, for the same reason liveness is not read from a pid.
Index ¶
- Constants
- func RunJob(dir string) int
- func ValidID(id string) bool
- type Exit
- type Job
- type Spec
- type Started
- type State
- type Store
- func (s *Store) Dir(id string) (string, error)
- func (s *Store) List() ([]string, error)
- func (s *Store) Remove(id string) error
- func (s *Store) Result(id string, max int64) (workspace.ExecResult, Exit, error)
- func (s *Store) Root() string
- func (s *Store) Start(ctx context.Context, spec Spec, stdin []byte) (Job, error)
- func (s *Store) Status(id string) (State, *Exit, error)
- func (s *Store) Stop(id string) error
Constants ¶
const DefaultKillGrace = 10 * time.Second
DefaultKillGrace is how long a command has between the wrapper's TERM and its KILL, at the deadline or on a stop.
const DefaultMaxLogBytes int64 = 16 << 20
DefaultMaxLogBytes caps each of a job's output files. The command keeps running past the cap; what it writes after is counted and dropped.
const HiddenCommand = "__job"
HiddenCommand is the txco subcommand that runs a job's wrapper: `txco __job <job directory>`.
Variables ¶
This section is empty.
Functions ¶
func RunJob ¶
RunJob is the wrapper: the body of `txco __job <dir>`. It runs the job described in dir to its end and records the exit there. Its return value is the wrapper's own exit status, which nothing reads: the job's outcome is exit.json.
It is safe to run twice on one directory. The second finds the lock held, or the exit already written, and does nothing.
Types ¶
type Exit ¶
type Exit struct {
// Exit is the command's exit code; -1 when a signal ended it or it never
// started.
Exit int `json:"exit"`
// TimedOut: the wrapper ended the command at its deadline.
TimedOut bool `json:"timed_out,omitempty"`
// Stopped: the wrapper ended the command because it was asked to.
Stopped bool `json:"stopped,omitempty"`
// StartError: the command could not be started at all.
StartError string `json:"start_error,omitempty"`
// Signal names the signal that ended the command, when one did.
Signal string `json:"signal,omitempty"`
StartedAt time.Time `json:"started_at"`
EndedAt time.Time `json:"ended_at"`
WallMS int64 `json:"wall_ms"`
// What the command wrote, and whether the output files' cap dropped any.
StdoutBytes int64 `json:"stdout_bytes"`
StderrBytes int64 `json:"stderr_bytes"`
StdoutTruncated bool `json:"stdout_truncated,omitempty"`
StderrTruncated bool `json:"stderr_truncated,omitempty"`
}
Exit is a job's outcome, as the wrapper recorded it.
type Spec ¶
type Spec struct {
Argv []string `json:"argv"`
Dir string `json:"dir"`
Env []string `json:"env"`
Deadline time.Time `json:"deadline"`
KillGraceMS int64 `json:"kill_grace_ms,omitempty"`
MaxLogBytes int64 `json:"max_log_bytes,omitempty"`
HasStdin bool `json:"has_stdin,omitempty"`
}
Spec is what a job runs. Env is the command's whole environment, already resolved by the caller: the wrapper adds nothing to it.
type Started ¶
type Started struct {
WrapperPID int `json:"wrapper_pid"`
CommandPID int `json:"command_pid"`
StartedAt time.Time `json:"started_at"`
}
Started is the wrapper's word that the command is running.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is a directory of jobs.
func New ¶
New returns the store rooted at root (created if needed, mode 0700). wrapper is the argv that runs a job's wrapper, the job's directory being appended to it; nil means this binary's own hidden command.
func (*Store) Remove ¶
Remove deletes a job's directory. A running job is refused: its wrapper would go on writing into nothing.
func (*Store) Result ¶
Result is an ended job's outcome in an exec's shape: the exit, and the first max bytes of each output (max <= 0: the workspace default).
func (*Store) Start ¶
Start launches spec as a detached job and returns once its command is running. A command that cannot be started (no such program, no such directory) is a bad_request, reported here and not as a job.
ctx bounds only the wait for the start; it is never the job's life. The job's life is spec.Deadline, which is required.