Builds: design 47 G (G-22 lead answers 2) A-448, A-449, A-449a; §153, §200, §579
215 lines
6.6 KiB
Go
215 lines
6.6 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
|
|
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"
|
|
)
|
|
|
|
// Status is a step's status for one artifact of a module, read from the module's registration and never from a
|
|
// manifest (design 47 G §21, §23): the artifact's own status where its policy names one, else the module's. A step
|
|
// whose status neither names is `optional` (§553): it runs and a failure holds nothing; an empty approval is no step (A-196).
|
|
func Status(reg catalog.Registration, artifact, step string) string {
|
|
if a, ok := reg.Policy.Artifacts[artifact]; ok {
|
|
if s := statusOf(a, step); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
if s := statusOf(reg.Policy.Steps, step); s != "" {
|
|
return s
|
|
}
|
|
if step == seat.StepResultStepApproval {
|
|
return ""
|
|
}
|
|
return seat.StepResultStatusOptional
|
|
}
|
|
|
|
func statusOf(s catalog.StepStatuses, step string) string {
|
|
switch step {
|
|
case seat.StepResultStepLint:
|
|
return s.Lint
|
|
case seat.StepResultStepUnit:
|
|
return s.Unit
|
|
case seat.StepResultStepIntegration:
|
|
return s.Integration
|
|
case seat.StepResultStepComposition:
|
|
return s.Composition
|
|
case seat.StepResultStepReview:
|
|
return s.Review
|
|
case seat.StepResultStepApproval:
|
|
return s.Approval
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// buildStep is the acting rule's step a build seat names for the languages it builds (design 47 G §323).
|
|
const buildStep = "build"
|
|
|
|
// BuildSeat is the build seat that runs lint and the test steps of an artifact of language, read from the catalog half
|
|
// of the pipeline's revision view (design 47 G §323): the seat whose acting rule `build` has `on` naming that language,
|
|
// as an entry of the half that claims it carries it in `acts`. Two seats naming one language are an error naming both.
|
|
func BuildSeat(entries map[string]catalog.CatalogEntry, language string) (string, error) {
|
|
var seats []string
|
|
for _, e := range entries {
|
|
for _, r := range e.Acts {
|
|
if r.Step == buildStep && r.On == language && r.Seat != "" && !contains(seats, r.Seat) {
|
|
seats = append(seats, r.Seat)
|
|
}
|
|
}
|
|
}
|
|
sort.Strings(seats)
|
|
switch len(seats) {
|
|
case 1:
|
|
return seats[0], nil
|
|
case 0:
|
|
return "", fmt.Errorf("no build seat's acting rule %s names the language %q in the catalog half", buildStep, language)
|
|
}
|
|
return "", fmt.Errorf("the build seats %v each name the language %q in their acting rule %s", seats, language, buildStep)
|
|
}
|
|
|
|
func contains(list []string, s string) bool {
|
|
for _, x := range list {
|
|
if x == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// BranchView is the runner's view of one watched branch's catalog half (design 47 G §195, §200, §479): a first view, then
|
|
// that branch's catalog-revision events. meshcatalogclient.Catalog is one.
|
|
type BranchView interface {
|
|
Hear(ctx context.Context, data []byte) error
|
|
View() (Half, bool)
|
|
}
|
|
|
|
// readyView is a BranchView that says when it holds its first half, as meshcatalogclient.Catalog does.
|
|
type readyView interface{ Ready() <-chan struct{} }
|
|
|
|
// Half is the catalog half a branch view holds, as the runner reads it: its revision and its entries by module.
|
|
type Half struct {
|
|
Revision int64
|
|
Source int64
|
|
Entries map[string]catalog.CatalogEntry
|
|
}
|
|
|
|
// Views is the runner's views of the catalog half, one per watched branch the registrations name (design 47 G §21,
|
|
// §195): a branch first named by a registration gets its view made and started by Open, and every catalog-revision
|
|
// heard goes to every branch's view, each taking its own branch's alone.
|
|
type Views struct {
|
|
// Open makes and starts the view of a branch; its error is said in the journal and the branch asked again at the
|
|
// next registration heard.
|
|
Open func(branch string) (BranchView, error)
|
|
// OnReady is told once for a branch, when its view holds a first half: the steps placed in the pipelines of that branch
|
|
// and not yet asked are asked then (§88's restart pickup).
|
|
OnReady func(branch string)
|
|
Logf func(string, ...any)
|
|
|
|
mu sync.Mutex
|
|
branches map[string]BranchView
|
|
}
|
|
|
|
// Registered takes the watched branches of a module's registration (design 47 G §21): each branch the views do not hold
|
|
// yet is opened.
|
|
func (v *Views) Registered(reg catalog.Registration) {
|
|
for _, b := range reg.Branches {
|
|
v.ensure(b)
|
|
}
|
|
}
|
|
|
|
func (v *Views) ensure(branch string) {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
if v.branches == nil {
|
|
v.branches = map[string]BranchView{}
|
|
}
|
|
if _, ok := v.branches[branch]; ok || branch == "" {
|
|
return
|
|
}
|
|
bv, err := v.Open(branch)
|
|
if err != nil {
|
|
if v.Logf != nil {
|
|
v.Logf("the catalog half of the watched branch %s is not followed (asked again at the next registration heard): %v", branch, err)
|
|
}
|
|
return
|
|
}
|
|
v.branches[branch] = bv
|
|
if r, ok := bv.(readyView); ok && v.OnReady != nil {
|
|
on := v.OnReady
|
|
go func() {
|
|
<-r.Ready()
|
|
on(branch)
|
|
}()
|
|
}
|
|
}
|
|
|
|
// Branches are the watched branches the views hold, sorted.
|
|
func (v *Views) Branches() []string {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
out := make([]string, 0, len(v.branches))
|
|
for b := range v.branches {
|
|
out = append(out, b)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// View is the catalog half the view of branch holds; false while it has none, when the runner acts on nothing new of
|
|
// that branch (design 47 G §200).
|
|
func (v *Views) View(branch string) (Half, bool) {
|
|
v.mu.Lock()
|
|
bv := v.branches[branch]
|
|
v.mu.Unlock()
|
|
if bv == nil {
|
|
return Half{}, false
|
|
}
|
|
return bv.View()
|
|
}
|
|
|
|
// HearRevision hands one catalog-revision event to every branch's view.
|
|
func (v *Views) HearRevision(ctx context.Context, data []byte) error {
|
|
v.mu.Lock()
|
|
all := make([]BranchView, 0, len(v.branches))
|
|
for _, bv := range v.branches {
|
|
all = append(all, bv)
|
|
}
|
|
v.mu.Unlock()
|
|
var first error
|
|
for _, bv := range all {
|
|
if err := bv.Hear(ctx, data); err != nil && first == nil {
|
|
first = err
|
|
}
|
|
}
|
|
return first
|
|
}
|
|
|
|
// Readiness tells, from the revisions a branch's view is seen holding, when the view first holds a half and when it reaches
|
|
// a newer revision: the first hold asks again the steps already placed (§88's restart pickup), a newer revision only places
|
|
// the steps of pipelines that waited for it (A-448, §200). It is safe for concurrent use.
|
|
type Readiness struct {
|
|
mu sync.Mutex
|
|
held bool
|
|
rev int64
|
|
}
|
|
|
|
// Observe notes that the view holds revision rev: first is true once, at the first hold; advanced is true when rev is newer
|
|
// than any seen before and it is not the first.
|
|
func (r *Readiness) Observe(rev int64) (first, advanced bool) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if !r.held {
|
|
r.held, r.rev = true, rev
|
|
return true, false
|
|
}
|
|
if rev > r.rev {
|
|
r.rev = rev
|
|
return false, true
|
|
}
|
|
return false, false
|
|
}
|