github.com/rotari-dev/rotari/internal/jobstatus

Overview

package jobstatus // import "github.com/rotari-dev/rotari/internal/jobstatus"

Package jobstatus resolves job outcomes from persisted run state.

It owns the read-side fallback chain shared by the CLI and Web projections: an
attempt's own `status` file, then its wrapper `status.json` once terminal, then
the persisted `scheduler_status.json`, and finally the run's recorded result:
its `summary.json` result, or before the summary the carried result it recorded
when it started (RecordedResults). It also resolves displayed job timestamps,
including the fallback to a carried job's origin. Callers render a resolution;
they should not re-implement any step of the chain or change run state.

Constants

const (
	ArtifactFile      = "file"
	ArtifactDirectory = "directory"
	ArtifactOther     = "other"
	ArtifactMissing   = "missing"
	// ArtifactUnknown is a relative path whose base was never known.
	ArtifactUnknown = "unknown"
)
    Types an ArtifactEntry observes at its path on the viewing host.

const StatusPending = "pending"
    StatusPending is the display status of a job rotari has not dispatched yet,
    such as one waiting for a dependency. A job queued by a scheduler is
    "waiting (recorded)" instead.

Functions

func Artifacts(store state.Store, runsDir string, origin model.JobOrigin) (artifact.Record, bool)
    Artifacts returns the artifact candidates recorded for the attempt that
    origin names under runsDir. A job carried into origin's run is read from the
    attempt that produced its result, as FilterJob reads it. ok is false when
    that attempt has no record, as in state written before discovery existed or
    for an attempt that never started: discovery information is unavailable,
    which does not mean the job had no associated files.

func AttemptAttributes(store state.Store, attemptDir string, now time.Time) (jobfilter.Attributes, error)
    AttemptAttributes reads the hosts and times an attempt directory records:
    the wrapper's start, or the submission when it has not started. A file that
    does not exist records nothing; one that cannot be read or decoded is an
    error naming it.

func AttemptID(latestAttemptID string, recorded model.JobResult, hasRecorded bool) string
    AttemptID is the attempt a job's row shows: its latest attempt in the run,
    which exists from dispatch on, or without one the attempt its recorded
    result names, as for a carried job.

func DisplayLabel(status string, accepted, carried bool) string
    DisplayLabel is the label views show for a job: its DisplayStatus, or
    success (accepted) for an accepted earlier failure, marked when the result
    was carried from another run.

func Elapsed(submitted, finished time.Time, done bool, now time.Time) time.Duration
    Elapsed is how long a job has run: from its submission to its finish, or to
    now while it runs. It is negative when either end is unknown (a zero time).

func FilterJob(store state.Store, runsDir string, origin model.JobOrigin, id string, result model.JobResult, finished bool, now time.Time) jobfilter.Job
    FilterJob returns what job filters need to know about the job that origin
    names under runsDir: result and finished as the caller resolved them, and
    the hosts, times, and log of the attempt that produced them. A job carried
    into origin's run is read from the run it was carried from. id is the job's
    ID for definition conditions. Hosts recorded on result take precedence over
    the attempt's, and times the carry records fill in ones the attempt lacks.

    A fact that was never recorded is left out; one recorded in a file that
    cannot be read or decoded, including the carry that locates the attempt,
    is an error naming the job and the file.

func LastOutputAt(jobDir string) (time.Time, bool)
    LastOutputAt returns when the attempt in jobDir last wrote to its output
    logs, the latest modification time of its merged or separate stdout and
    stderr files, and false when it has written none. A running job whose last
    output is long past may be stuck. Logs an executor keeps on another host are
    not seen.

func ReadLog(attemptDir string) (log string, found bool, err error)
    ReadLog returns the log of an attempt for diagnosis: its merged output,
    or else its stdout, or else its stderr. found is false when the attempt has
    none of them; a log file that exists but cannot be read is an error.

func ReadStatusFile(jobDir string) (int, bool)
    ReadStatusFile reads the attempt's plain `status` exit-code file.

func RecordedResults(run state.RunSnapshot) map[string]model.JobResult
    RecordedResults returns the results a run has recorded for its jobs, which
    the chain reads as each job's run result: its summary's once it has one,
    and before that the results it carries forward from earlier runs, which it
    records before dispatching any job. A job the run executes has no recorded
    result until the summary.

func ResultAttemptDir(runsDir string, origin model.JobOrigin) (string, error)
    ResultAttemptDir returns the directory of the attempt that produced the
    result origin names: that attempt, or for a job carried into origin's run,
    the attempt it was carried from, as FilterJob reads it. It is "" when no
    such directory exists, and an error when a carry cannot be read.

func Timestamps(runDir, jobDir string, origin *model.JobOrigin, carried bool) (submittedAt, finishedAt string)
    Timestamps returns a job's submitted and finished times. A carried result
    uses timestamps from its origin; a job executed in this run uses only its
    own latest attempt's, read from jobDir as state.LatestAttempt returns it.

func WithoutRecordedMark(label string) string
    WithoutRecordedMark drops the mark every recorded unfinished state carries,
    for a compact view where it tells no two states apart.

func WrapperTerminal(status executor.WrapperStatus) bool
    WrapperTerminal reports whether a wrapper `status.json` records a finished
    job.

Types

type ArtifactEntry struct {
	Path string `json:"path"`
	// DisplayPath is Path relative to the listing's working directory when
	// under it, and Path otherwise.
	DisplayPath string `json:"display_path"`
	Basis       string `json:"basis"`
	Type        string `json:"type"`
	// Origin is a one-line account of where the candidate was found.
	Origin  string            `json:"origin"`
	Sources []artifact.Source `json:"sources"`
}
    ArtifactEntry is one candidate with what the viewing host finds at its path
    now. A missing path may exist on another host, such as an SSH job's.

type ArtifactListing struct {
	// Recorded is false when the attempt has no record: discovery
	// information is unavailable, unlike a record with no candidates.
	Recorded bool `json:"recorded"`
	Version  int  `json:"version,omitempty"`
	// WorkingDirectory is the directory relative references were resolved
	// on, when the record names it.
	WorkingDirectory string `json:"working_directory,omitempty"`
	// WorkingDirectoryType is what the viewing host finds at an absolute
	// WorkingDirectory now, as Type is for an entry.
	WorkingDirectoryType string                `json:"working_directory_type,omitempty"`
	Entries              []ArtifactEntry       `json:"entries,omitempty"`
	Diagnostics          []artifact.Diagnostic `json:"diagnostics,omitempty"`
}
    ArtifactListing is an attempt's artifact candidates as the CLI, JSON,
    and Web UI show them.

func ListArtifacts(store state.Store, runsDir string, origin model.JobOrigin) ArtifactListing
    ListArtifacts returns the listing of the attempt origin names, read like
    Artifacts, with each path observed now. It follows symlinks and never opens
    a path.

type Attempt struct {
	ExitCode int
	Source   Source
	// HasAttempt indicates an existing concrete attempt directory,
	// even when it has not yet written a status file. False is not evidence
	// that the job has never started.
	HasAttempt bool
	// Undispatched means the job's directory in an existing run directory
	// is absent or holds only a cancel marker. Executors write attempt
	// records before starting a job, so this is positive evidence that no
	// attempt was dispatched.
	Undispatched bool
	// Wrapper is the attempt's `status.json`, valid when HasWrapper is set,
	// whether or not its phase is terminal.
	Wrapper        executor.WrapperStatus
	HasWrapper     bool
	SchedulerState string
}
    Attempt is what one attempt directory records about its outcome.

func ReadAttempt(store state.Store, jobDir string) Attempt
    ReadAttempt resolves an attempt directory's outcome: `status`, then a
    terminal wrapper `status.json`, then a terminal `scheduler_status.json`.

func (attempt Attempt) Finished() bool
    Finished reports whether the attempt has a terminal exit code.

func (attempt Attempt) Result(job model.JobSpec) (model.JobResult, bool)
    Result returns the attempt's outcome as a job result, or false while the
    attempt has not finished.

type Job struct {
	ExitCode   int
	Source     Source
	Attempt    Attempt
	Summary    model.JobResult
	HasSummary bool
}
    Job is the resolved outcome of a job in one run: its latest attempt first,
    then the run's `summary.json` result.

func ReadJob(store state.Store, jobDir string, summary model.JobResult, hasSummary bool) Job
    ReadJob reads the job's attempt directory and resolves it against the job's
    summary result, if any.

func ResolveAttempt(attempt Attempt, latest bool, summary model.JobResult, hasSummary bool) Job
    ResolveAttempt resolves a displayed attempt of a job. The run's summary
    result belongs to the job's latest attempt, so an older attempt resolves
    from its own files only and stays unfinished without a terminal state.

func ResolveJob(attempt Attempt, summary model.JobResult, hasSummary bool) Job
    ResolveJob applies the job-level fallback chain to an attempt already read
    with ReadAttempt and the job's summary result, if any.

func (job Job) Accepted() bool
    Accepted reports whether the run accepted the job's earlier failure as a
    success.

func (job Job) Blocked() bool
    Blocked reports whether the job never ran because a dependency failed.

func (job Job) DisplayStatus(spec model.JobSpec) string
    DisplayStatus classifies the latest persisted state for human/API views.
    Nonterminal executor states are explicitly marked as recorded, not live.
    A job directory missing from its run is pending; other missing or unusable
    records are unknown.

func (job Job) Finished() bool
    Finished reports whether any step of the chain has a terminal exit code.

func (job Job) Hosts() []string
    Hosts returns the summary's hosts, falling back to the wrapper's.

func (job Job) Result(spec model.JobSpec) (model.JobResult, bool)
    Result returns the resolved outcome as a job result, or false while the job
    has not finished. A summary result keeps its metadata, such as acceptance
    and diagnoses, with the resolved exit code.

type RunTime struct {
	// Elapsed runs from submission to the finish, or to now while the job
	// runs. It is negative when either end is unknown.
	Elapsed time.Duration
	// Running is true for a submitted job that has not finished.
	Running bool
	// Quiet is, for a running job, how long ago it last wrote output. It is
	// negative when the job has written none, or is not running.
	Quiet time.Duration
}
    RunTime is how long a job has run and, while it runs, how long ago it last
    wrote output.

func MeasureRunTime(jobDir string, submitted, finished time.Time, done bool, now time.Time) RunTime
    MeasureRunTime measures the attempt in jobDir from its submission and finish
    times; a zero time is unknown. Every view of a job's run time, `show` and
    `jobs` alike, measures through it.

type Source int
    Source names the step of the fallback chain that supplied an exit code.

const (
	// SourceNone means no step has a terminal outcome yet.
	SourceNone Source = iota
	// SourceStatus is the attempt's plain `status` exit-code file.
	SourceStatus
	// SourceWrapper is the attempt's wrapper `status.json` in a terminal phase.
	SourceWrapper
	// SourceScheduler is a terminal state in the attempt's
	// `scheduler_status.json`.
	SourceScheduler
	// SourceSummary is the job's result in the run's `summary.json`.
	SourceSummary
)