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 defaults (design 47 H2 H-A-467 (1)): the branch main and the newest 20. const ( DefaultPipelinesBranch = "main" 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 one branch's list as the page shows it: the rows list answered, or why it could not be read. type PipelinesPage struct { Branch string `json:"branch"` 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 followed beside the default; nil for none logf func(string, ...any) dirty chan struct{} mu sync.Mutex lists map[string]PipelinesPage // by branch, the branches followed 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 list is // followed beside the default branch's, 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 one branch's newest limit pipelines in its short form; a limit below one is the // default's. func (p *Pipelines) List(branch string, limit int) ([]seat.PipelineRow, error) { if limit < 1 { limit = DefaultPipelinesLimit } raw, err := p.call(PipelinesListVerb, seat.ListInput{Branch: branch, Limit: int64(limit)}) if err != nil { return nil, fmt.Errorf("%s of %s: %w", PipelinesListVerb, branch, err) } answer, said, ok := buscalls.Unwrap(raw) if !ok { return nil, fmt.Errorf("%s of %s answered: %s", PipelinesListVerb, branch, 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, branch, 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}) 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 default branch and the watched branches, each once. func (p *Pipelines) followed() []string { out := []string{DefaultPipelinesBranch} if p.branches != nil { for _, b := range p.branches() { if b != "" && !slices.Contains(out, b) { out = append(out, b) } } } return out } // Page is one branch's newest limit pipelines (the default's below one): a followed branch's at the default limit as last // read, any other branch or limit read now. func (p *Pipelines) Page(branch string, limit int) PipelinesPage { if branch = strings.TrimSpace(branch); branch == "" { branch = DefaultPipelinesBranch } if limit < 1 { limit = DefaultPipelinesLimit } if limit == DefaultPipelinesLimit { p.mu.Lock() page, ok := p.lists[branch] p.mu.Unlock() if ok { return page } } return p.read(branch, limit) } func (p *Pipelines) read(branch string, limit int) PipelinesPage { rows, err := p.List(branch, limit) page := PipelinesPage{Branch: branch, 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 followed branch again and says the lists changed. func (p *Pipelines) Refresh() { 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.lists = lists p.mu.Unlock() p.notify("") } // Of is the pipeline of a delivery: of the branch's followed list, 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() } for _, r := range page.Rows { // newest first, as list answers them if source != 0 && 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 } // 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.heard[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.heard[what] > since p.mu.Unlock() if changed { return true } } } }