# Conflicts: # modules/mesh-delivery-contracts/seats/mesh-pipeline-runner/seat.json # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/main.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/steps.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/verbs.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/verbs_test.go # modules/mesh-delivery-pipeline/cmd/mesh-delivery-pipeline/views.go # modules/mesh-delivery-pipeline/internal/pipeline/start.go # modules/mesh-delivery-pipeline/manifest_test.go # modules/mesh-delivery-pipeline/module.json
265 lines
9.8 KiB
Go
265 lines
9.8 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
runnerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshpipelinerunnerclient"
|
|
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// RunGroupPrefix is a run on request's candidate group before its id, `run-<id>` (design 47 G A-160).
|
|
const RunGroupPrefix = "run-"
|
|
|
|
// RunKey is the key of a run on request's pipeline, `<branch>.<id>.<module>`: the run's id under the member's named branch
|
|
// (design 47 G §420, A-193).
|
|
func RunKey(branch, run, module string) string {
|
|
return seat.PipelinesByBranchRevisionModuleKey(keys.EscapeName(branch), run, module)
|
|
}
|
|
|
|
// NewRunID is a run's id, a random 12-hex id, so two runs of one commit are two pipelines (A-180).
|
|
func NewRunID() (string, error) {
|
|
b := make([]byte, 6)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(b), nil
|
|
}
|
|
|
|
// Run starts a run on request (design 47 G §64, §420; A-180, A-193): it writes each member's pipeline at once, running and
|
|
// with no steps, its `run` the run's new id, and says run-started, from which the indexer builds the run's candidate; the
|
|
// candidate's revision places the steps (PlaceRun). A member naming no module, commit or branch is refused, the run then
|
|
// writing nothing. caller is the one the bus proved. newID makes the run's id (NewRunID).
|
|
func (s *Starter) Run(caller string, in seat.RunInput, newID func() (string, error)) (seat.RunAnswer, error) {
|
|
if len(in.Members) == 0 {
|
|
return seat.RunAnswer{}, errors.New("a run names one or more members, each a module at a commit on a branch")
|
|
}
|
|
for _, m := range in.Members {
|
|
if m.Module == "" || m.Commit == "" || m.Branch == "" {
|
|
return seat.RunAnswer{}, fmt.Errorf("a run's member names its module, commit and branch: %+v", m)
|
|
}
|
|
}
|
|
id, err := newID()
|
|
if err != nil {
|
|
return seat.RunAnswer{}, fmt.Errorf("the run's id is not made: %w", err)
|
|
}
|
|
now := s.now()
|
|
var written []string
|
|
for _, m := range in.Members {
|
|
key := RunKey(m.Branch, id, m.Module)
|
|
p := seat.Pipeline{V: 1, Pipeline: key, Branch: m.Branch, Module: m.Module, Commit: m.Commit, Run: id,
|
|
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}}
|
|
raw, err := json.Marshal(p)
|
|
if err == nil {
|
|
err = s.Pipelines.Create(key, raw)
|
|
}
|
|
if err != nil {
|
|
s.failRun(written, "the run's other pipelines were not written")
|
|
return seat.RunAnswer{}, fmt.Errorf("the pipeline %s of the run is not written: %w", key, err)
|
|
}
|
|
written = append(written, key)
|
|
}
|
|
first := in.Members[0]
|
|
last := first.Commit
|
|
if len(in.Members) > 1 {
|
|
last = RunGroupPrefix + id
|
|
}
|
|
ev := seat.RunStarted{V: 1, At: &now, By: caller, Members: in.Members, Run: id}
|
|
if err := s.Say(runnerclient.Subject(seat.EventRunStarted, first.Module, first.Branch, last), ev); err != nil {
|
|
s.failRun(written, "run-started was not said, so no candidate is built for it")
|
|
return seat.RunAnswer{}, fmt.Errorf("the run %s is not started: run-started is not said: %w", id, err)
|
|
}
|
|
return seat.RunAnswer{Run: id, Pipelines: written}, nil
|
|
}
|
|
|
|
// failRun ends the run's pipelines written before its start failed, failed and saying why, so none stands running.
|
|
func (s *Starter) failRun(written []string, why string) {
|
|
if s.Store == nil {
|
|
return
|
|
}
|
|
for _, key := range written {
|
|
for {
|
|
p, rev, err := s.Store.Get(key)
|
|
if err != nil || p.Outcome != seat.PipelineOutcomeRunning {
|
|
break
|
|
}
|
|
now := s.now()
|
|
p.Outcome, p.Why, p.Ended = seat.PipelineOutcomeFailed, why, &now
|
|
p.EndCount++
|
|
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
|
|
continue
|
|
} else if err != nil {
|
|
s.logf("the pipeline %s of a run not started stays running: %v", key, err)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// PlaceRun places a run's pipelines from its candidate's revision (design 47 G §64, §373, A-180, A-193): ev is the
|
|
// catalog-revision of the candidate `run-<id>`, half its entries read whole through the seat's revision verb. Each member's
|
|
// pipeline of the run not yet placed takes its fingerprint, the candidate's revision and its steps, by compare-and-set, and
|
|
// says pipeline-started; each further entry whose fingerprint the members change (another than in base, the branch's catalog
|
|
// half, or absent there) gets a pipeline of the run of its own, placed the same way. Another candidate's revision places
|
|
// nothing; a revision heard again places nothing twice. It says how many pipelines it placed.
|
|
func (s *Starter) PlaceRun(ev catalog.CatalogRevisionEvent, half Half, base func(branch string) (Half, bool)) (int, error) {
|
|
id, ok := strings.CutPrefix(ev.Group, RunGroupPrefix)
|
|
if !ok || id == "" {
|
|
return 0, nil
|
|
}
|
|
if s.Store == nil {
|
|
return 0, errors.New("the runner holds no pipelines to place a run's in")
|
|
}
|
|
pkeys, err := s.Store.Keys("*." + id + ".*")
|
|
if err != nil {
|
|
return 0, fmt.Errorf("the pipelines of the run %s are not listed: %w", id, err)
|
|
}
|
|
slices.Sort(pkeys)
|
|
members := map[string]bool{}
|
|
branch := ""
|
|
for _, key := range pkeys {
|
|
if p, _, err := s.Store.Get(key); err == nil && p.Run == id {
|
|
members[p.Module] = true
|
|
if branch == "" {
|
|
branch = p.Branch
|
|
}
|
|
}
|
|
}
|
|
if branch == "" {
|
|
s.logf("the candidate %s's revision %d names a run with no pipeline here: nothing is placed", ev.Group, ev.Revision)
|
|
return 0, nil
|
|
}
|
|
placed := 0
|
|
var first error
|
|
keep := func(ok bool, err error) {
|
|
if err != nil && first == nil {
|
|
first = err
|
|
}
|
|
if ok {
|
|
placed++
|
|
}
|
|
}
|
|
for _, key := range pkeys {
|
|
keep(s.placeRunPipeline(key, id, ev.Revision, half))
|
|
}
|
|
// The dependents (§373): the candidate's entries the members change, beside the members, on the first member's branch.
|
|
trunk, ok := base(branch)
|
|
if b, err := keys.UnescapeName(ev.Branch); err == nil && b != "" && b != branch {
|
|
if h, held := base(b); held {
|
|
trunk, ok = h, true
|
|
}
|
|
}
|
|
if !ok {
|
|
s.logf("the run %s's dependents are not known: the catalog half of %s is not held, so its members run alone", id, branch)
|
|
return placed, first
|
|
}
|
|
for _, module := range slices.Sorted(maps.Keys(half.Entries)) {
|
|
e := half.Entries[module]
|
|
if members[module] {
|
|
continue
|
|
}
|
|
if was, held := trunk.Entries[module]; held && was.Fp == e.Fp {
|
|
continue
|
|
}
|
|
key := RunKey(branch, id, module)
|
|
now := s.now()
|
|
p := seat.Pipeline{V: 1, Pipeline: key, Branch: branch, Module: module, Commit: e.Commit, Run: id,
|
|
Outcome: seat.PipelineOutcomeRunning, Started: &now, Steps: []seat.StepResult{}}
|
|
raw, err := json.Marshal(p)
|
|
if err == nil {
|
|
err = s.Pipelines.Create(key, raw)
|
|
}
|
|
if err != nil && !errors.Is(err, ErrExists) {
|
|
keep(false, fmt.Errorf("the dependent %s of the run %s is not written: %w", module, id, err))
|
|
continue
|
|
}
|
|
keep(s.placeRunPipeline(key, id, ev.Revision, half))
|
|
}
|
|
return placed, first
|
|
}
|
|
|
|
// UnplacedRun is a run with a pipeline running and no steps placed yet, on the branch the run names.
|
|
type UnplacedRun struct{ Run, Branch string }
|
|
|
|
// UnplacedRuns are the runs with a pipeline running and no steps placed yet, by branch and id (design 47 G A-222): a placing
|
|
// lost while the runner was down is taken up from these at its start.
|
|
func (s *Starter) UnplacedRuns() ([]UnplacedRun, error) {
|
|
if s.Store == nil {
|
|
return nil, errors.New("the runner holds no pipelines to read a run's from")
|
|
}
|
|
pkeys, err := s.Store.Keys("*.*.*")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the pipelines are not listed: %w", err)
|
|
}
|
|
var out []UnplacedRun
|
|
for _, key := range pkeys {
|
|
if p, _, err := s.Store.Get(key); err == nil && p.Run != "" && p.Outcome == seat.PipelineOutcomeRunning && p.Revision == 0 {
|
|
if u := (UnplacedRun{Run: p.Run, Branch: p.Branch}); !slices.Contains(out, u) {
|
|
out = append(out, u)
|
|
}
|
|
}
|
|
}
|
|
slices.SortFunc(out, func(a, b UnplacedRun) int { return strings.Compare(a.Branch+"\n"+a.Run, b.Branch+"\n"+b.Run) })
|
|
return out, nil
|
|
}
|
|
|
|
// placeRunPipeline places one pipeline of the run, written and not yet placed (no revision), from the candidate's half.
|
|
func (s *Starter) placeRunPipeline(key, id string, revision int64, half Half) (bool, error) {
|
|
for {
|
|
p, rev, err := s.Store.Get(key)
|
|
if err != nil {
|
|
return false, fmt.Errorf("the pipeline %s is not read: %w", key, err)
|
|
}
|
|
if p.Run != id || p.Outcome != seat.PipelineOutcomeRunning || p.Revision != 0 {
|
|
return false, nil
|
|
}
|
|
now := s.now()
|
|
e, ok := half.Entries[p.Module]
|
|
if !ok {
|
|
s.logf("the candidate of the run %s holds no entry of %s: its pipeline is placed with no fingerprint", id, p.Module)
|
|
}
|
|
p.Fingerprint, p.Repository, p.Revision = e.Fp, e.Repository, revision
|
|
var asks []Asked
|
|
if s.Registration != nil {
|
|
if reg, ok := s.Registration(p.Module); ok {
|
|
p.Steps, asks = Place(p, reg, half.Entries, now, s.Name)
|
|
} else {
|
|
s.logf("%s has no registration yet: its pipeline is placed with no step", key)
|
|
}
|
|
}
|
|
if _, err := s.Store.Update(key, p, rev); errors.Is(err, ErrChanged) {
|
|
continue
|
|
} else if err != nil {
|
|
return false, fmt.Errorf("the pipeline %s cannot be written: %w", key, err)
|
|
}
|
|
ev := seat.PipelineStarted{V: 1, At: &now, Pipeline: key, Branch: p.Branch, Module: p.Module, Commit: p.Commit,
|
|
Fingerprint: p.Fingerprint, Revision: p.Revision, Run: id}
|
|
if err := s.Say(runnerclient.Subject(seat.EventPipelineStarted, p.Module, p.Branch, p.Commit), ev); err != nil {
|
|
s.logf("pipeline-started of %s is not said: %v", key, err)
|
|
}
|
|
if s.Steps != nil && len(asks) > 0 {
|
|
s.carry(key, asks)
|
|
if err := askAll(s.Steps, s.Arm, key, asks, s.recovered(key, p.Commit)); err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
func (s *Starter) now() time.Time {
|
|
if s.Now != nil {
|
|
return s.Now().UTC()
|
|
}
|
|
return time.Now().UTC()
|
|
}
|