detach

package
v0.2.63 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MPL-2.0 Imports: 17 Imported by: ? 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 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

View Source
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.

View Source
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.

View Source
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

func RunJob(dir string) int

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.

func ValidID

func ValidID(id string) bool

ValidID reports whether id has a job id's shape. Every method that takes an id checks it, so an id can never name a path outside the store.

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 Job

type Job struct {
	ID        string
	Dir       string
	StartedAt time.Time
}

Job is a started job.

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 State

type State string

State is where a job stands.

const (
	// Running: the wrapper holds the job's lock.
	Running State = "running"
	// Ended: exit.json is there.
	Ended State = "ended"
	// Lost: no exit, and no wrapper. Whether the command finished is unknown.
	Lost State = "lost"
)

type Store

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

Store is a directory of jobs.

func New

func New(root string, wrapper []string) (*Store, error)

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) Dir

func (s *Store) Dir(id string) (string, error)

Dir is the job's directory, or an error for a malformed id.

func (*Store) List

func (s *Store) List() ([]string, error)

List returns the ids of every job in the store, oldest name first.

func (*Store) Remove

func (s *Store) Remove(id string) error

Remove deletes a job's directory. A running job is refused: its wrapper would go on writing into nothing.

func (*Store) Result

func (s *Store) Result(id string, max int64) (workspace.ExecResult, Exit, error)

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) Root

func (s *Store) Root() string

Root is the store's directory.

func (*Store) Start

func (s *Store) Start(ctx context.Context, spec Spec, stdin []byte) (Job, error)

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.

func (*Store) Status

func (s *Store) Status(id string) (State, *Exit, error)

Status says where a job stands, with its exit once it has ended.

func (*Store) Stop

func (s *Store) Stop(id string) error

Stop asks a job's wrapper to end its command: TERM to the command's group, then KILL after the job's grace. The exit then says stopped. Asking twice, or asking of a job that has ended, changes nothing.

Jump to

Keyboard shortcuts

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