engine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 21 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 engine keeps the live state of a runner (local logs + optional org polling), publishes changes to subscribers and executes actions. The daemon, the web UI and the in-process TUI all sit on top of it.

Index

Constants

View Source
const (
	ActRerunJob      = "rerun-job"
	ActRerunFailed   = "rerun-failed"
	ActRerunRun      = "rerun-run"
	ActCancelRun     = "cancel-run"
	ActRunnerStart   = "runner-start"
	ActRunnerStop    = "runner-stop"
	ActRunnerRestart = "runner-restart"
	ActRunnerAbort   = "runner-abort-pending"
)

Variables

This section is empty.

Functions

func Outcome

func Outcome(j JobView) string

Outcome is a job's display outcome: running, abandoned, unknown or the lower-cased runner result (succeeded, failed, canceled).

func RunKey

func RunKey(j JobView) string

RunKey identifies a job's run attempt. Jobs whose run is unknown (damaged logs) each get their own group instead of being lumped together.

Types

type Action

type Action struct {
	Kind  string `json:"kind"`
	Repo  string `json:"repo,omitempty"`
	RunID string `json:"run_id,omitempty"`
	JobID string `json:"job_id,omitempty"` // REST job id (check_run_id)
	Force bool   `json:"force,omitempty"`  // runner stop/restart even if a job is running
}

Action is a request from a client.

type Backend

type Backend interface {
	State(ctx context.Context) (*State, error)
	// Watch delivers a value whenever the state may have changed. The channel
	// is closed when ctx ends or the backend goes away.
	Watch(ctx context.Context) <-chan struct{}
	Do(ctx context.Context, a Action) (string, error)
	// Usage is the live CPU/memory of the runner and its running job.
	Usage(ctx context.Context) (*Usage, error)
}

Backend is what UIs talk to: the in-process Engine or a remote daemon.

type Engine

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

func New

func New(cfg config.Config, opts Options) *Engine

func (*Engine) Do

func (e *Engine) Do(ctx context.Context, a Action) (string, error)

Do implements Backend. Every action is checked against the current state: only runs and jobs cothon is showing can be acted on.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, ready chan<- struct{}) error

Run watches the runner until ctx is canceled. The first scan happens before Run returns its first state, so callers can call State right after Ready is closed.

func (*Engine) State

func (e *Engine) State(context.Context) (*State, error)

State implements Backend.

func (*Engine) Usage

func (e *Engine) Usage(context.Context) (*Usage, error)

Usage implements Backend: the latest live sample.

func (*Engine) Watch

func (e *Engine) Watch(ctx context.Context) <-chan struct{}

Watch implements Backend.

type JobUsage

type JobUsage struct {
	JobID   string       `json:"job_id"`
	Current ProcUsage    `json:"current"`
	Summary UsageSummary `json:"summary"`
	History []UsagePoint `json:"history"`
}

type JobView

type JobView struct {
	*fleet.Job
	HTMLURL string        `json:"html_url"`
	RunURL  string        `json:"run_url"`
	Usage   *UsageSummary `json:"usage,omitempty"` // finished jobs measured by cothon
}

JobView is a job plus derived links.

type Notice

type Notice struct {
	At      time.Time `json:"at"`
	OK      bool      `json:"ok"`
	Message string    `json:"message"`
}

Notice is feedback for the last action.

type Options

type Options struct {
	// Notify enables desktop notifications (daemon / web modes).
	Notify bool
	// Org enables org polling when the config allows it.
	Org bool
	Log *slog.Logger
}

type OrgJob

type OrgJob struct {
	ID         string    `json:"id"`
	Name       string    `json:"name"`
	Status     string    `json:"status"`
	Conclusion string    `json:"conclusion,omitempty"`
	RunnerName string    `json:"runner_name,omitempty"`
	Labels     []string  `json:"labels"`
	HTMLURL    string    `json:"html_url"`
	Started    time.Time `json:"started,omitzero"`
	// OnThisRunner: running here, or queued with labels this runner serves.
	OnThisRunner bool `json:"on_this_runner"`
}

type OrgRun

type OrgRun struct {
	Repo      string    `json:"repo"`
	Workflow  string    `json:"workflow"`
	RunID     string    `json:"run_id"`
	RunNumber int       `json:"run_number"`
	Attempt   int       `json:"run_attempt"`
	Status    string    `json:"status"` // queued | in_progress | …
	Branch    string    `json:"branch"`
	Event     string    `json:"event"`
	Actor     string    `json:"actor"`
	HTMLURL   string    `json:"html_url"`
	Created   time.Time `json:"created"`
	Jobs      []OrgJob  `json:"jobs"`
}

type OrgState

type OrgState struct {
	Name          string    `json:"name"`
	Updated       time.Time `json:"updated,omitzero"`
	Warning       string    `json:"warning,omitempty"`
	Runs          []OrgRun  `json:"runs"` // queued / in-progress runs, newest first
	RateRemaining int       `json:"rate_remaining"`
}

OrgState is the optional organization-wide view.

type ProcUsage

type ProcUsage struct {
	CPU   float64 `json:"cpu"` // percent of one core, like top
	RSS   uint64  `json:"rss"` // bytes
	Procs int     `json:"procs"`
}

type QueueState

type QueueState struct {
	Jobs    []QueuedJob `json:"jobs"` // oldest first: the next one to start is on top
	Updated time.Time   `json:"updated,omitzero"`
	Warning string      `json:"warning,omitempty"`
}

QueueState lists jobs waiting for a runner with this runner's labels.

type QueuedJob

type QueuedJob struct {
	Repo      string    `json:"repo"`
	Workflow  string    `json:"workflow"`
	RunID     string    `json:"run_id"`
	RunNumber int       `json:"run_number"`
	JobID     string    `json:"job_id"`
	Name      string    `json:"name"`
	Branch    string    `json:"branch"`
	Event     string    `json:"event"`
	Actor     string    `json:"actor"`
	HTMLURL   string    `json:"html_url"`
	Queued    time.Time `json:"queued"` // when the job was created
	Labels    []string  `json:"labels"`
}

type RepoGroup

type RepoGroup struct {
	Repo                          string
	Runs                          []RunGroup
	Latest                        time.Time
	Outcome                       string
	OK, Failed, Canceled, Running int
}

RepoGroup is a repository with its runs, newest first.

func GroupJobs

func GroupJobs(jobs []JobView) []RepoGroup

GroupJobs groups jobs (newest first) into repos → runs → jobs.

func GroupRepos

func GroupRepos(runs []RunGroup) []RepoGroup

GroupRepos nests runs under their repo; repos ordered by latest activity.

type RunGroup

type RunGroup struct {
	Key                                       string
	Repo, Workflow, RunID, RunNumber, Attempt string
	Ref, Event, Actor, RunURL                 string
	Jobs                                      []JobView // execution order
	Started, Finished                         time.Time
	Outcome                                   string // worst outcome: running > failed > abandoned > canceled > succeeded
	OK, Failed, Canceled, Running             int
}

RunGroup is one workflow run attempt and the jobs this runner executed for it.

func GroupRuns

func GroupRuns(jobs []JobView) []RunGroup

GroupRuns groups jobs by (repo, run, attempt), newest run first.

func (RunGroup) Span

func (g RunGroup) Span() time.Duration

Span is the wall time from the first job start to the last job end (or now).

type State

type State struct {
	Version   uint64              `json:"version"`
	Updated   time.Time           `json:"updated"`
	Runner    *fleet.RunnerConfig `json:"runner,omitempty"`
	RunnerDir string              `json:"runner_dir"`
	Listener  fleet.ListenerState `json:"listener"`
	Processes fleet.Processes     `json:"processes"`
	// Pending is a deferred runner operation waiting for the current job to
	// finish: "", "stop" or "restart".
	Pending  string      `json:"pending,omitempty"`
	Jobs     []JobView   `json:"jobs"` // newest first
	Org      *OrgState   `json:"org,omitempty"`
	Queue    *QueueState `json:"queue,omitempty"` // jobs waiting for this runner (nil: queue polling off)
	Warnings []string    `json:"warnings,omitempty"`
	Notice   *Notice     `json:"notice,omitempty"` // result of the last action
}

State is an immutable snapshot published to clients. Version increases whenever anything visible changes.

func (*State) RunnerStatus

func (s *State) RunnerStatus() string

RunnerStatus summarizes the runner for headers and icons.

func (*State) RunningJob

func (s *State) RunningJob() *JobView

RunningJob returns the job currently executing, if any.

type Usage

type Usage struct {
	Runner ProcUsage `json:"runner"`        // listener + run.sh themselves, without jobs
	Job    *JobUsage `json:"job,omitempty"` // the running job's process tree
	Cores  int       `json:"cores"`
	At     time.Time `json:"at"`
}

Usage is the live view.

type UsagePoint

type UsagePoint struct {
	T   time.Time `json:"t"`
	CPU float64   `json:"cpu"`
	RSS uint64    `json:"rss"`
}

type UsageSummary

type UsageSummary struct {
	PeakRSS    uint64  `json:"peak_rss"`
	PeakCPU    float64 `json:"peak_cpu"`
	AvgCPU     float64 `json:"avg_cpu"`     // percent of one core over the sampled time
	CPUSeconds float64 `json:"cpu_seconds"` // total CPU time
	Samples    int     `json:"samples"`
}

UsageSummary is what a job used overall.

Jump to

Keyboard shortcuts

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