hostjobs

package
v0.331.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 11 Imported by: 0 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 hostjobs is the store of SCHEDULED HOST JOBS (migration 0166; docs/host-jobs.md): each job as its host last reported it, its cron's next fire, and its runs. The protocol is internal/mirror's hostjobs.go, the host half internal/mirrorhost's, and what a fire and a result DO — the trigger, the page version, the Attention item — internal/api's hostjobs.go.

🔴 A JOB IS WRITTEN HERE ONLY FROM ITS HOST'S REPORT (Report). Nothing in this package takes a command from anyone else, and nothing reads one back to send it anywhere: the command column is for display.

🔴 EVERY METHOD RUNS UNDER THE CALLER'S PRINCIPAL (row-level security, owner_id defaulting to the principal) — except the scheduler's FireDue and the sweep's LostRuns, which read every owner's rows through this package's one System helper (sys) and hand each row back with its owner, to be acted on as THAT owner.

Index

Constants

View Source
const (
	StatusRunning = "running"
	// StatusOK: the command exited 0 and its output was a valid page — a
	// version written, or none because it was unchanged.
	StatusOK = "ok"
	// StatusFailed: a non-zero exit, a stdout over the bound, or a page that
	// could not be written.
	StatusFailed = "failed"
	// StatusTimeout: the host stopped it at its timeout.
	StatusTimeout = "timeout"
	// StatusInvalid: it exited 0 and its output is not a valid page.
	StatusInvalid = "invalid"
	// StatusOffline: the host was not connected when it was due.
	StatusOffline = "offline"
	// StatusSkipped: it was due while its previous run was still going.
	StatusSkipped = "skipped"
	// StatusRefused: the host refused the job-run (Detail says why).
	StatusRefused = "refused"
	// StatusLost: muster never got its result (LostRuns).
	StatusLost = "lost"
	// StatusWaiting: the command exited ExitWaiting — it is waiting on
	// setup (its prerequisites are missing), which is not a failure (0204).
	StatusWaiting = "waiting"
)

Run statuses (0166's CHECK).

View Source
const (
	TriggerCron   = "cron"
	TriggerManual = "manual"
)

Triggers (0166's CHECK).

View Source
const (
	StatePaused        = "Paused"
	StateExpectFailure = "fails on purpose (test)"
)

The job list's words for a job's switches (JobState).

View Source
const ExitWaiting = 75

ExitWaiting is the exit status a job's command uses to say "waiting on setup": EX_TEMPFAIL (sysexits.h). Its run is StatusWaiting.

View Source
const LostRunGrace = 5 * time.Minute

LostRunGrace is how long past its timeout a running run waits for its result before LostRuns calls it lost: the host's own kill, its result's trip, and a reconnect.

Variables

View Source
var (
	// ErrNotFound is a job or run that is not the caller's, or not there.
	ErrNotFound = errors.New("hostjobs: no such job or run")
)

Functions

func CleanDetail

func CleanDetail(s string) string

CleanDetail is a run's detail: valid UTF-8, at most 1000 characters (0166's CHECK).

func CleanTail

func CleanTail(s string) string

CleanTail is stderr as a run row keeps it: valid UTF-8 with no NUL, at most mirror.MaxJobStderrTail bytes (0166's CHECK), cut at its START (it is a tail).

func Ignored added in v0.322.0

func Ignored(st string) bool

Ignored reports whether a run of status st says nothing about whether the job works: a skip, a run still going, or one waiting on setup. A failure streak and a job's last success are counted over the other runs (Summarize).

func JobState added in v0.322.0

func JobState(paused, expectFailure bool) string

JobState is how the job list shows a job paused and/or failing on purpose, "" for neither.

Types

type Finish

type Finish struct {
	Status      string
	StartedAt   *time.Time
	EndedAt     time.Time
	ExitCode    *int
	DurationMs  *int64
	PageID      int64
	PageVersion int
	StderrTail  string
	Detail      string
}

Finish is how a run ended, as FinishRun records it.

type Fire

type Fire struct {
	Job   Job
	DueAt time.Time
}

Fire is one due job FireDue fired: the job (its owner set) and the time it was due.

type Job

type Job struct {
	ID      int64    `json:"id"`
	Owner   int64    `json:"-"`
	Host    string   `json:"host"`
	Name    string   `json:"name"`
	Cron    string   `json:"cron"`
	Command []string `json:"command"`
	// PageID is the page muster made for the job on its first output (0:
	// none yet). Every job writes its own page; a host names none.
	PageID      int64      `json:"page_id,omitempty"`
	Title       string     `json:"title,omitempty"`
	Output      string     `json:"output"`
	TimeoutS    int        `json:"timeout_seconds"`
	Declared    bool       `json:"declared"`
	NextFireAt  *time.Time `json:"next_fire_at"`
	LastFiredAt *time.Time `json:"last_fired_at"`
	ReportedAt  time.Time  `json:"reported_at"`
	// Paused: its owner paused it (SetPaused, 0204); the scheduler does not
	// fire it. Muster's, never the host's.
	Paused bool `json:"paused"`
	// ExpectFailure: its host declared it as failing on purpose (a test job,
	// mirror.HostJob.ExpectFailure): a failed run raises no item.
	ExpectFailure bool `json:"expect_failure"`
	// State is how the job list shows Paused and ExpectFailure (JobState).
	State string `json:"state"`
}

Job is one job as its host last reported it.

func (Job) Decl

func (j Job) Decl() mirror.HostJob

Decl is j as its host declared it (mirror.HostJob).

type Run

type Run struct {
	ID          int64      `json:"id"`
	Owner       int64      `json:"-"`
	JobID       int64      `json:"job_id"`
	Host        string     `json:"host"`
	Job         string     `json:"job"`
	Trigger     string     `json:"trigger"`
	Status      string     `json:"status"`
	TimeoutS    int        `json:"timeout_seconds"`
	FiredAt     time.Time  `json:"fired_at"`
	StartedAt   *time.Time `json:"started_at"`
	EndedAt     *time.Time `json:"ended_at"`
	ExitCode    *int       `json:"exit_code"`
	DurationMs  *int64     `json:"duration_ms"`
	PageID      int64      `json:"page_id,omitempty"`
	PageVersion int        `json:"page_version,omitempty"`
	StderrTail  string     `json:"stderr_tail,omitempty"`
	Detail      string     `json:"detail,omitempty"`
	NoticeID    int64      `json:"notice_id,omitempty"`
}

Run is one run of a job.

type Store

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

Store is the two tables on Postgres.

func NewPG

func NewPG(pool *pgxpool.Pool) *Store

NewPG returns a Store on pool.

func (*Store) ClaimRun

func (s *Store) ClaimRun(ctx context.Context, id int64, now time.Time) (bool, error)

ClaimRun claims the ending of the caller's RUNNING run id at now (its ended_at, until FinishRun records the rest): true for the ONE caller that claims it — before any page is written for it — false when it is claimed or ended already. A claimed run is never called lost.

func (*Store) FinishRun

func (s *Store) FinishRun(ctx context.Context, id int64, f Finish) (Run, bool, error)

FinishRun moves the caller's RUNNING run id to f, and answers it — false when it was not running (finished already, by a late duplicate result or the lost sweep): exactly one ending is recorded, and only that caller acts on it.

func (*Store) FireDue

func (s *Store) FireDue(ctx context.Context, now time.Time, limit int) ([]Fire, error)

FireDue advances every job due at now — a PAUSED one is never due (SetPaused) — at most limit, each at most once — a compare-and-set on next_fire_at (… WHERE id = $1 AND next_fire_at = <the value read>), so of two replicas reading the same due job exactly one moves it — and answers the jobs THIS caller moved. Missed fires collapse to one: the next fire is computed from now, as an officer schedule's is. ctx must act for every owner: it is read through sys.

func (*Store) Forget

func (s *Store) Forget(ctx context.Context, host, name string) ([]int64, error)

Forget deletes the caller's job name on host — declared or not — with its runs (0166's cascade), and answers the Attention items its runs recorded (the caller withdraws the open ones). ErrNotFound when the caller has no such job. A host that still declares it records it again on its next connection: forget is for a host that is gone.

func (*Store) History added in v0.322.0

func (s *Store) History(ctx context.Context, job, through int64) ([]Run, error)

History is the caller's job's runs up to and including run through, newest first: every run the job keeps (maxRunsPerJob), so a streak and the last success are counted over the whole history (Summarize).

func (*Store) Job

func (s *Store) Job(ctx context.Context, id int64) (Job, error)

Job is the caller's job id.

func (*Store) Jobs

func (s *Store) Jobs(ctx context.Context) ([]Job, error)

Jobs is the caller's DECLARED jobs, by host then name.

func (*Store) JobsNamed

func (s *Store) JobsNamed(ctx context.Context, name, host string) ([]Job, error)

JobsNamed is the caller's declared jobs named name, on host when host is not "" (a name may be declared on several hosts).

func (*Store) LastGood added in v0.322.0

func (s *Store) LastGood(ctx context.Context, job int64) (Run, error)

LastGood is the caller's job's newest ok run that WROTE a page version (a run whose output was unchanged wrote none: the version before it is the same output), ErrNotFound for none: what "Open the last good output" opens.

func (*Store) LostRuns

func (s *Store) LostRuns(ctx context.Context, now time.Time, limit int) ([]Run, error)

LostRuns moves every owner's running run whose result is overdue at now (fired_at + its timeout + LostRunGrace) to lost, at most limit, and answers those this caller moved, each with its owner. Read and moved through sys: each is then acted on as its owner.

func (*Store) NoticesSince added in v0.322.0

func (s *Store) NoticesSince(ctx context.Context, job, run int64) ([]int64, error)

NoticesSince is the distinct Attention items the caller's job's runs recorded after its newest ok run before run (all of them when it has none): what a run that succeeded withdraws.

func (*Store) OfflineNotices

func (s *Store) OfflineNotices(ctx context.Context, host string, job int64) ([]int64, error)

OfflineNotices is the distinct Attention items the caller's OFFLINE runs of host's jobs recorded (of job only, when job > 0).

func (*Store) OpenOfflineNotice

func (s *Store) OpenOfflineNotice(ctx context.Context, job, except int64) (int64, error)

OpenOfflineNotice is the Attention item the caller's job's latest OFFLINE run before run except recorded (0: none): what a consecutive offline fire shares rather than filing its own. Only a run AFTER the job's last run of any other status counts: once the job ran (or failed otherwise), the next offline fire is a new episode.

func (*Store) Report

func (s *Store) Report(ctx context.Context, host string, jobs []mirror.HostJob, now time.Time) error

Report records the caller's host's declared jobs (CleanJobs' list): each is created or updated as reported, and every other job of that host is UNDECLARED (kept for its runs, never fired). A job whose cron is new or changed, or that was undeclared, gets its next fire computed from now; an unchanged one keeps the fire it had, so a reconnect neither skips nor repeats a fire. A job whose cron does not parse is not recorded (and is named in the error; the rest are).

func (*Store) Run

func (s *Store) Run(ctx context.Context, id int64) (Run, error)

Run is the caller's run id.

func (*Store) RunByNotice added in v0.322.0

func (s *Store) RunByNotice(ctx context.Context, ask int64) (Run, error)

RunByNotice is the caller's newest run that recorded Attention item ask (consecutive offline runs share one), ErrNotFound for none: what a job notice's card and buttons act on. Row-level security makes another owner's item find none.

func (*Store) Runs

func (s *Store) Runs(ctx context.Context, job int64, limit int) ([]Run, error)

Runs is the caller's job id's runs, newest first, at most limit.

func (*Store) SetNotice

func (s *Store) SetNotice(ctx context.Context, id, ask int64) (bool, error)

SetNotice records Attention item ask as run id's — once: false when the run has one already (another path raised it first; the caller withdraws its copy).

func (*Store) SetPage

func (s *Store) SetPage(ctx context.Context, id, page int64) error

SetPage records page as the one muster made for the caller's job id.

func (*Store) SetPaused added in v0.322.0

func (s *Store) SetPaused(ctx context.Context, id int64, paused bool, now time.Time) (Job, error)

SetPaused pauses (or resumes) the caller's job id at now, and answers it. A paused job is never fired by the scheduler (FireDue); a run by hand still runs. RESUMING computes its next fire from now, as a new cron's is, so a job resumed after its due time passed does not fire at once for the time it was paused. ErrNotFound when it is not the caller's.

func (*Store) StartRun

func (s *Store) StartRun(ctx context.Context, j Job, trigger string, now time.Time, online bool) (Run, error)

StartRun records a run of the caller's job j by trigger at now, as ONE of:

  • skipped, when a run of j is still running (the 0166 one-running index decides, whichever replica or path got there first);
  • running, when online (the caller then sends the host its job-run);
  • offline, when the host is not connected.

type Summary added in v0.322.0

type Summary struct {
	// LastOK is the job's newest ok run before it, nil for none.
	LastOK *Run
	// Streak is how many failures in a row end with it (1: the first);
	// 0 when the run is ok or Ignored.
	Streak int
	// GoodBefore is how many ok runs in a row came before the streak.
	GoodBefore int
	// FirstRun: no run before it counts (Ignored ones aside).
	FirstRun bool
}

Summary is a run's place in its job's history, as its notice says it (Summarize).

func Summarize added in v0.322.0

func Summarize(runs []Run) Summary

Summarize is the summary of runs[0], runs being its job's runs up to it, newest first (History). Skipped, running and waiting runs are passed over (Ignored): they say nothing about whether the job works.

Jump to

Keyboard shortcuts

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