Files
archie/modules/runs/stages.go
T
sam 682995bdf7 feat(runs): workflow drafts, publish with an expected version, delete, enabled flag, run on demand, run stages and statistics (#2061, #2113, #2098)
A disabled workflow is never started by a trigger, another workflow or a
person. Delete is refused while a trigger, another workflow or a run in
progress names it. A parked or ended run restarts from a step.
2026-11-22 11:34:49 +11:00

158 lines
4.1 KiB
Go

package runs
import (
json "encoding/json/v2"
"slices"
"strings"
"time"
"github.com/samcharles93/archie-core/modules/runs/internal/store"
)
// The states a step of a run is in.
const (
StagePending = "pending"
StageRunning = "running"
StageWaiting = "waiting"
StageSucceeded = "succeeded"
StageFailed = "failed"
StageSkipped = "skipped"
StageCancelled = "cancelled"
)
// Stage is how far one top-level step of a run's document has got.
type Stage struct {
// Name is the step's id, or its type and position when it has none, as the
// trail names it.
Name string `json:"name"`
Type string `json:"type"`
State string `json:"state"`
StartedAt *time.Time `json:"started_at,omitzero"`
EndedAt *time.Time `json:"ended_at,omitzero"`
Error string `json:"error,omitzero"`
}
// region is the span of recorded steps one top-level step made, which begins at
// the position its marker records.
type region struct {
name string
first int
at time.Time
}
// regions reads the markers a run left where each top-level step began, in the
// order the steps began.
func regions(steps []store.Recorded) []region {
var out []region
for _, s := range steps {
name, ok := strings.CutPrefix(s.Name, enteredPrefix)
if !ok || s.Seq < markerBase {
continue
}
var first int
if json.Unmarshal(s.Output, &first) != nil {
continue
}
out = append(out, region{name: name, first: first, at: s.RecordedAt})
}
return out
}
// stageOf is the region step seq falls in: the last one that began at or before it.
func stageOf(regs []region, seq int) string {
name := ""
for _, r := range regs {
if r.first <= seq {
name = r.name
}
}
return name
}
// stagesOf reads how far each top-level step of doc got in a run from what the
// run recorded. A step the run never reached is pending while the run goes on
// and skipped once it has ended.
func stagesOf(doc Document, steps []store.Recorded, run RunView) []Stage {
regs := regions(steps)
skipped := map[string]bool{}
for _, s := range steps {
if name, ok := strings.CutPrefix(s.Name, "skip:"); ok && s.Seq < markerBase {
skipped[name] = true
}
}
out := make([]Stage, 0, len(doc.Steps))
for i, step := range doc.Steps {
name := step.name(i)
st := Stage{Name: name, Type: stepKind(step), State: StagePending}
if terminal(run.State) {
st.State = StageSkipped
}
if at := slices.IndexFunc(regs, func(r region) bool { return r.name == name }); at >= 0 {
reached(&st, regs, at, skipped[name], run)
}
out = append(out, st)
}
return out
}
// reached fills in a stage for the step that began at regs[at].
func reached(st *Stage, regs []region, at int, skipped bool, run RunView) {
started := regs[at].at
st.StartedAt = &started
last := at == len(regs)-1
switch {
case skipped:
st.State = StageSkipped
case !last:
st.State = StageSucceeded
default:
st.State, st.Error = lastStage(st.Name, run)
}
switch {
case !last:
next := regs[at+1].at
st.EndedAt = &next
case run.FinishedAt != nil && st.State != StageRunning && st.State != StageWaiting:
st.EndedAt = run.FinishedAt
}
}
// lastStage is the state of the step a run reached last, which the run's own
// state decides.
func lastStage(name string, run RunView) (state, failure string) {
switch run.State {
case store.StateQueued, store.StateRunning:
return StageRunning, ""
case store.StateWaiting, store.StateWaitingApproval:
return StageWaiting, ""
case store.StateCancelled:
return StageCancelled, ""
case store.StateFailed:
return StageFailed, run.Error
case store.StateParked:
if reason, ok := strings.CutPrefix(run.Detail, "step "+name+": "); ok {
return StageFailed, reason
}
}
return StageSucceeded, ""
}
func terminal(state string) bool {
switch state {
case store.StateQueued, store.StateRunning, store.StateWaiting, store.StateWaitingApproval:
return false
}
return true
}
// stepKind is the type of a step, or the kind of branching it makes.
func stepKind(s Step) string {
switch {
case s.Switch != nil:
return "switch"
case s.Parallel != nil:
return "parallel"
}
return s.Type
}