The join with CT14 (08812c7f) renames fp to fingerprint in the runner's Pipeline, PipelineStarted, PipelineEnded; the readers follow. Builds: design 47 G §534, §550, §585, §646 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
137 lines
5.3 KiB
Go
137 lines
5.3 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
runnerclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshpipelinerunnerclient"
|
|
builds "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshbuilds"
|
|
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshpipelinerunner"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// ErrExists is a Create of a pipeline key that is written already: the second start, which writes nothing (§54).
|
|
var ErrExists = errors.New("the pipeline's key is written already")
|
|
|
|
// Created is the runner's state pipelines as a start writes it: a key written only when it is absent (compare-and-set on
|
|
// its absence), ErrExists when it is not.
|
|
type Created interface {
|
|
Create(key string, value []byte) error
|
|
}
|
|
|
|
// Begin is what a start names: the pipeline's key (watched branch, source revision, module; design 47 G §27, §54) and
|
|
// what the catalog half of that branch's view says of the module.
|
|
type Begin struct {
|
|
Branch string
|
|
Source int64
|
|
Module string
|
|
Commit string
|
|
Fp string
|
|
Group string
|
|
Repository string
|
|
Revision int64
|
|
}
|
|
|
|
// Key is a pipeline's key in the state pipelines, `<branch>.<source revision>.<module>`, the branch escaped as a key's
|
|
// name is (§27).
|
|
func Key(branch string, source int64, module string) string {
|
|
return seat.PipelinesByBranchRevisionModuleKey(keys.EscapeName(branch), strconv.FormatInt(source, 10), module)
|
|
}
|
|
|
|
// Starter starts pipelines (design 47 G §54, §103): it writes a pipeline's key by compare-and-set on its absence, so one
|
|
// source revision and module has one pipeline whichever event starts it first, and says pipeline-started for the one it
|
|
// wrote.
|
|
type Starter struct {
|
|
Pipelines Created
|
|
// Say says one of the seat's events on its subject.
|
|
Say func(subject string, body any) error
|
|
Now func() time.Time
|
|
Logf func(string, ...any)
|
|
}
|
|
|
|
// Start writes b's pipeline, running, and says pipeline-started; false, nil when its key was written already (the second
|
|
// start writes nothing, §54).
|
|
func (s *Starter) Start(b Begin) (bool, error) {
|
|
if b.Branch == "" || b.Module == "" || b.Source <= 0 {
|
|
return false, fmt.Errorf("a pipeline names its watched branch, source revision and module: %+v", b)
|
|
}
|
|
now := time.Now().UTC()
|
|
if s.Now != nil {
|
|
now = s.Now().UTC()
|
|
}
|
|
key := Key(b.Branch, b.Source, b.Module)
|
|
p := seat.Pipeline{V: 1, Pipeline: key, Branch: b.Branch, Source: b.Source, Module: b.Module, Commit: b.Commit, Fingerprint: b.Fp,
|
|
Group: b.Group, Repository: b.Repository, Revision: b.Revision, Outcome: seat.PipelineOutcomeRunning, Started: &now,
|
|
Steps: []seat.StepResult{}}
|
|
raw, err := json.Marshal(p)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if err := s.Pipelines.Create(key, raw); errors.Is(err, ErrExists) {
|
|
s.logf("%s is started already: this start writes nothing", key)
|
|
return false, nil
|
|
} else if err != nil {
|
|
return false, fmt.Errorf("the pipeline %s is not written: %w", key, err)
|
|
}
|
|
ev := seat.PipelineStarted{V: 1, At: &now, Pipeline: key, Branch: b.Branch, Source: b.Source, Module: b.Module,
|
|
Commit: b.Commit, Fingerprint: b.Fp, Group: b.Group, Revision: b.Revision}
|
|
commitOrGroup := b.Commit
|
|
if b.Group != "" {
|
|
commitOrGroup = b.Group
|
|
}
|
|
if err := s.Say(runnerclient.Subject(seat.EventPipelineStarted, b.Module, b.Branch, commitOrGroup), ev); err != nil {
|
|
s.logf("pipeline-started of %s is not said: %v", key, err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (s *Starter) logf(format string, args ...any) {
|
|
if s.Logf != nil {
|
|
s.Logf(format, args...)
|
|
}
|
|
}
|
|
|
|
// outdatedKind is the edge `outdated` names a dependent behind by for a rebuild on a publish: B published before A builds,
|
|
// a package A imports (§53).
|
|
const outdatedKind = 1
|
|
|
|
// Published starts, for a package-published, a pipeline for each dependent the catalog's `outdated` names behind for that
|
|
// package on that branch by kind 1, keyed by the publication's watched branch and source revision and the dependent
|
|
// (design 47 G §53, §334). outdated is the kept call of the verb; view the catalog half of the publication's branch. A
|
|
// dependent the half does not hold is said and started with what the event names.
|
|
func (s *Starter) Published(ev builds.PackagePublished, outdated func(catalog.OutdatedInput) (catalog.OutdatedAnswer, error), view func(branch string) (Half, bool)) (int, error) {
|
|
if ev.Artifact == "" || ev.Branch == "" || ev.Source <= 0 {
|
|
return 0, fmt.Errorf("a package-published names its artifact, watched branch and source revision: %+v", ev)
|
|
}
|
|
a, err := outdated(catalog.OutdatedInput{Branch: ev.Branch, Dependency: ev.Artifact})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("outdated of %s on %s: %w", ev.Artifact, ev.Branch, err)
|
|
}
|
|
half, _ := view(ev.Branch)
|
|
started := 0
|
|
var first error
|
|
for _, o := range a.Behind {
|
|
if o.Kind != outdatedKind || o.Module == "" {
|
|
continue
|
|
}
|
|
b := Begin{Branch: ev.Branch, Source: ev.Source, Module: o.Module, Revision: half.Revision}
|
|
if e, ok := half.Entries[o.Module]; ok {
|
|
b.Commit, b.Fp, b.Repository = e.Commit, e.Fp, e.Repository
|
|
} else {
|
|
s.logf("the catalog half of %s holds no entry of %s, behind %s: its pipeline starts without its commit", ev.Branch, o.Module, ev.Artifact)
|
|
}
|
|
ok, err := s.Start(b)
|
|
if err != nil && first == nil {
|
|
first = err
|
|
}
|
|
if ok {
|
|
started++
|
|
}
|
|
}
|
|
return started, first
|
|
}
|