The operator found a pipelines view filtered by branch name makes no sense: the page and the terminal view now list the newest pipelines of every branch together, newest first, the branch a column, and offer one filter, a module (the pipelines that carry it). A delivery still links to its pipeline from its branch's list, or from every branch's list by the row's branch. Builds: design 47 H2 H-A-533 (hq 1b1e1984)
334 lines
11 KiB
Go
334 lines
11 KiB
Go
package view
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
"git.novox.be/novox/mesh-sdk/go/buscalls"
|
|
)
|
|
|
|
// The seat mesh-pipeline-runner's verbs list and show as the view's invokes names them: the pipelines page reads the
|
|
// pipelines through them and keeps no state of its own (design 47 H2 H-A-467 (1)).
|
|
const (
|
|
PipelinesListVerb = "seat:" + seat.Seat + "." + seat.VerbList
|
|
PipelinesShowVerb = "seat:" + seat.Seat + "." + seat.VerbShow
|
|
)
|
|
|
|
// The pipelines page's default (design 47 H2 H-A-467 (1)): the newest 20, of every branch (H-A-533).
|
|
const (
|
|
DefaultPipelinesLimit = 20
|
|
// DefaultPipelinesHeard is the default of the setting pipelines-heard: how many pipelines' last changes the view keeps
|
|
// for its pages before it forgets them all (principle 11).
|
|
DefaultPipelinesHeard = 1024
|
|
)
|
|
|
|
// PipelineEvents are the runner's events the pipelines page refreshes on (design 47 H2 H-A-467 (1)), never a timer.
|
|
var PipelineEvents = []string{seat.EventPipelineStarted, seat.EventStepEnded, seat.EventPipelineEnded, seat.EventRePinned}
|
|
|
|
// PipelineEventPatterns are the subjects of PipelineEvents on the process's hub: each event, with and without the names
|
|
// its sayer adds after the event's name.
|
|
func PipelineEventPatterns() []string {
|
|
var out []string
|
|
for _, e := range PipelineEvents {
|
|
out = append(out, "mesh.seat."+seat.Seat+".event."+e, "mesh.seat."+seat.Seat+".event."+e+".>")
|
|
}
|
|
return out
|
|
}
|
|
|
|
// PipelinesPage is the list as the page shows it (design 47 H2 H-A-533): the newest pipelines of every branch together,
|
|
// each row naming its branch, or only those that carry Module when one is named; the rows list answered, or why it could not
|
|
// be read.
|
|
type PipelinesPage struct {
|
|
Module string `json:"module,omitempty"`
|
|
Rows []seat.PipelineRow `json:"rows"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// Pipelines reads the runner's pipelines through its seat's verbs and says when they changed, on the events it hears
|
|
// (design 47 H2 H-A-467). It holds the last list it read of each branch it follows, the answer of a bus call and no state
|
|
// of the view's own: what it read is read again at each event.
|
|
type Pipelines struct {
|
|
call func(key string, body any) (json.RawMessage, error)
|
|
branches func() []string // the watched branches whose lists the deliveries' links read; nil for none
|
|
logf func(string, ...any)
|
|
dirty chan struct{}
|
|
|
|
mu sync.Mutex
|
|
all *PipelinesPage // every branch's newest, as last read; nil before the first reading
|
|
lists map[string]PipelinesPage // by branch, the watched branches, for the deliveries' links
|
|
subs map[chan string]struct{}
|
|
version uint64
|
|
heard map[string]uint64 // the version of the last change of each pipeline heard
|
|
most int // the pipelines heard kept before heard is forgotten whole: the setting pipelines-heard
|
|
}
|
|
|
|
// NewPipelines is a reader of the pipelines through call; branches, when given, names the watched branches whose lists are
|
|
// read for the deliveries' links (design 47 H2 H-A-467 (3)); heard is the setting pipelines-heard, its default below one.
|
|
func NewPipelines(call func(key string, body any) (json.RawMessage, error), branches func() []string, heard int, logf func(string, ...any)) *Pipelines {
|
|
if logf == nil {
|
|
logf = func(string, ...any) {}
|
|
}
|
|
if heard < 1 {
|
|
heard = DefaultPipelinesHeard
|
|
}
|
|
return &Pipelines{call: call, branches: branches, logf: logf, dirty: make(chan struct{}, 1),
|
|
lists: map[string]PipelinesPage{}, subs: map[chan string]struct{}{}, heard: map[string]uint64{}, most: heard}
|
|
}
|
|
|
|
// List asks the seat's verb list for the newest limit pipelines in its short form: of one branch when named, of every
|
|
// branch when not (H-A-533), and only those carrying module when named; a limit below one is the default's.
|
|
func (p *Pipelines) List(branch, module string, limit int) ([]seat.PipelineRow, error) {
|
|
if limit < 1 {
|
|
limit = DefaultPipelinesLimit
|
|
}
|
|
of := "every branch"
|
|
if branch != "" {
|
|
of = branch
|
|
}
|
|
if module != "" {
|
|
of += " carrying " + module
|
|
}
|
|
raw, err := p.call(PipelinesListVerb, seat.ListInput{Branch: branch, Module: module, Limit: int64(limit)})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s of %s: %w", PipelinesListVerb, of, err)
|
|
}
|
|
answer, said, ok := buscalls.Unwrap(raw)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s of %s answered: %s", PipelinesListVerb, of, said)
|
|
}
|
|
var a seat.ListAnswer
|
|
if err := json.Unmarshal(answer, &a); err != nil {
|
|
return nil, fmt.Errorf("the answer of %s of %s is not a list: %w", PipelinesListVerb, of, err)
|
|
}
|
|
return a.Rows, nil
|
|
}
|
|
|
|
// Show asks the seat's verb show for one pipeline: naming no mover, its movers' rows; naming one, that mover's steps
|
|
// (design 47 H2 H-A-467 (2)); next is the cursor of a page show answered.
|
|
func (p *Pipelines) Show(pipeline, mover, next string) (seat.ShowAnswer, error) {
|
|
raw, err := p.call(PipelinesShowVerb, seat.ShowInput{Pipeline: pipeline, Mover: mover, Next: next})
|
|
return showAnswer(raw, err, pipeline)
|
|
}
|
|
|
|
// showAnswer reads an answer of show.
|
|
func showAnswer(raw json.RawMessage, err error, pipeline string) (seat.ShowAnswer, error) {
|
|
if err != nil {
|
|
return seat.ShowAnswer{}, fmt.Errorf("%s of %s: %w", PipelinesShowVerb, pipeline, err)
|
|
}
|
|
answer, said, ok := buscalls.Unwrap(raw)
|
|
if !ok {
|
|
return seat.ShowAnswer{}, fmt.Errorf("%s of %s answered: %s", PipelinesShowVerb, pipeline, said)
|
|
}
|
|
var a seat.ShowAnswer
|
|
if err := json.Unmarshal(answer, &a); err != nil {
|
|
return a, fmt.Errorf("the answer of %s of %s is not a pipeline: %w", PipelinesShowVerb, pipeline, err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// followed is the watched branches, each once, whose lists the deliveries' links read.
|
|
func (p *Pipelines) followed() []string {
|
|
var out []string
|
|
if p.branches != nil {
|
|
for _, b := range p.branches() {
|
|
if b != "" && !slices.Contains(out, b) {
|
|
out = append(out, b)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Page is the newest limit pipelines of every branch (the default's below one), of those carrying module when named
|
|
// (design 47 H2 H-A-533): with no module at the default limit as last read, else read now.
|
|
func (p *Pipelines) Page(module string, limit int) PipelinesPage {
|
|
module = strings.TrimSpace(module)
|
|
if limit < 1 {
|
|
limit = DefaultPipelinesLimit
|
|
}
|
|
if module == "" && limit == DefaultPipelinesLimit {
|
|
p.mu.Lock()
|
|
all := p.all
|
|
p.mu.Unlock()
|
|
if all != nil {
|
|
return *all
|
|
}
|
|
}
|
|
return p.read("", module, limit)
|
|
}
|
|
|
|
func (p *Pipelines) read(branch, module string, limit int) PipelinesPage {
|
|
rows, err := p.List(branch, module, limit)
|
|
page := PipelinesPage{Module: module, Rows: rows}
|
|
if err != nil {
|
|
page.Error = err.Error()
|
|
}
|
|
if page.Rows == nil {
|
|
page.Rows = []seat.PipelineRow{}
|
|
}
|
|
return page
|
|
}
|
|
|
|
// Refresh reads the list of every branch, and of each watched branch for the deliveries' links, again and says the lists
|
|
// changed.
|
|
func (p *Pipelines) Refresh() {
|
|
all := p.read("", "", DefaultPipelinesLimit)
|
|
if all.Error != "" {
|
|
p.logf("the pipelines are not read (read again at the runner's next event): %s", all.Error)
|
|
}
|
|
lists := map[string]PipelinesPage{}
|
|
for _, b := range p.followed() {
|
|
page := p.read(b, "", DefaultPipelinesLimit)
|
|
if page.Error != "" {
|
|
p.logf("the pipelines of %s are not read (read again at the runner's next event): %s", b, page.Error)
|
|
}
|
|
lists[b] = page
|
|
}
|
|
p.mu.Lock()
|
|
p.all, p.lists = &all, lists
|
|
p.mu.Unlock()
|
|
p.notify("")
|
|
}
|
|
|
|
// Of is the pipeline of a delivery: of the branch's list, then of every branch's, the newest whose source is the delivery's source
|
|
// revision (design 47 H2 H-A-467 (3)); "" when none. A branch whose list is not read yet (a watched branch the reader
|
|
// learnt of after its last reading) has the lists read again, once.
|
|
func (p *Pipelines) Of(branch string, source uint64) string {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
page, ok := p.lists[branch]
|
|
if !ok {
|
|
p.owe()
|
|
}
|
|
if source == 0 {
|
|
return ""
|
|
}
|
|
for _, r := range page.Rows { // newest first, as list answers them
|
|
if r.Source == int64(source) {
|
|
return r.Pipeline
|
|
}
|
|
}
|
|
if p.all != nil {
|
|
for _, r := range p.all.Rows { // every branch's newest, each naming its branch (H-A-533)
|
|
if r.Branch == branch && r.Source == int64(source) {
|
|
return r.Pipeline
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Hear takes one event of the runner's (PipelineEvents): the pages of its pipeline are told at once, and the lists are
|
|
// read again by Run, which then tells the list pages.
|
|
func (p *Pipelines) Hear(data []byte) error {
|
|
var ev struct {
|
|
Pipeline string `json:"pipeline"`
|
|
}
|
|
if err := json.Unmarshal(data, &ev); err != nil || ev.Pipeline == "" {
|
|
return fmt.Errorf("not an event of a pipeline: %.200s", data)
|
|
}
|
|
p.notify(ev.Pipeline)
|
|
p.owe()
|
|
return nil
|
|
}
|
|
|
|
// deliveryKey is what heard keeps the last change of the delivery under: a want, a have or an assignment the view heard,
|
|
// which every pipeline's page shows (design 47 H2 H-A-531). No pipeline's key holds a NUL.
|
|
const deliveryKey = "\x00delivery"
|
|
|
|
// Delivered tells every pipeline's page that the delivery changed: a want, a have or an assignment the view heard, on the
|
|
// events it already hears for "What each node runs" (design 47 H2 H-A-531), never a timer. The lists are not read again.
|
|
func (p *Pipelines) Delivered() { p.notify(deliveryKey) }
|
|
|
|
// owe has Run read the lists again.
|
|
func (p *Pipelines) owe() {
|
|
select {
|
|
case p.dirty <- struct{}{}:
|
|
default: // a reading is owed already: it reads what this change asks too
|
|
}
|
|
}
|
|
|
|
// Run reads the lists at its start and again after each event heard, until ctx ends; events heard while it reads are
|
|
// read at once after, in one reading.
|
|
func (p *Pipelines) Run(ctx context.Context) {
|
|
p.Refresh()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-p.dirty:
|
|
p.Refresh()
|
|
}
|
|
}
|
|
}
|
|
|
|
// notify tells every subscriber what changed: a pipeline's key, or "" for the lists.
|
|
func (p *Pipelines) notify(what string) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
p.version++
|
|
if what != "" {
|
|
if _, known := p.heard[what]; !known && len(p.heard) >= p.most {
|
|
clear(p.heard)
|
|
}
|
|
p.heard[what] = p.version
|
|
} else {
|
|
p.heard[""] = p.version
|
|
}
|
|
for ch := range p.subs {
|
|
select {
|
|
case ch <- what:
|
|
default: // the subscriber is told already and has not looked yet
|
|
}
|
|
}
|
|
}
|
|
|
|
// Version is the count of changes said so far, given to a page so its subscription knows what it missed.
|
|
func (p *Pipelines) Version() uint64 {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
return p.version
|
|
}
|
|
|
|
// Changed waits until what (a pipeline's key, or "" for the lists) changes after the version since, or ctx ends: it
|
|
// answers at once when it changed already.
|
|
func (p *Pipelines) Changed(ctx context.Context, what string, since uint64) bool {
|
|
ch := make(chan string, 1)
|
|
p.mu.Lock()
|
|
if p.changedSince(what, since) {
|
|
p.mu.Unlock()
|
|
return true
|
|
}
|
|
p.subs[ch] = struct{}{}
|
|
p.mu.Unlock()
|
|
defer func() {
|
|
p.mu.Lock()
|
|
delete(p.subs, ch)
|
|
p.mu.Unlock()
|
|
}()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return false
|
|
case <-ch: // any change: a note dropped while one waited is read from heard, never lost
|
|
p.mu.Lock()
|
|
changed := p.changedSince(what, since)
|
|
p.mu.Unlock()
|
|
if changed {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// changedSince says whether what changed after since: a pipeline's page changes with the delivery too (design 47 H2
|
|
// H-A-531), the lists do not. The caller holds p.mu.
|
|
func (p *Pipelines) changedSince(what string, since uint64) bool {
|
|
return p.heard[what] > since || (what != "" && p.heard[deliveryKey] > since)
|
|
}
|