The checker's fixed names of the build modules (buildmodules.go in its program, rules and check) go: the Examiner finds each build module by the language its seat's acting rule builds (buildrun.BuildSeats over the seat list's definitions and the holders the indexer's modules name by their manifests' claims), the facts carry the language where a level counts by it, and the program's manifest reads the steps and toolchains of the seats and hears their toolchain-reported. The tools read the steps of each holder and follow the seat list as the view does. Timers stay (A-118). Builds: design 47 G A-82, A-118, §204 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
109 lines
4.1 KiB
Go
109 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"sync"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun"
|
|
)
|
|
|
|
// toolchainWatch keeps the build modules' toolchains in a buildrun.ToolchainSet and tells the builder when one changed. A
|
|
// build module's bucket is made when it is assigned, after the builder started, and the builder holds no timer (ADR 0437
|
|
// §8): it watches the buckets that exist at start, and a build module says each time it wrote its toolchains key
|
|
// (toolchain.reported, handled by Reported); a bucket not watched yet is then watched.
|
|
type toolchainWatch struct {
|
|
ctx context.Context
|
|
js bucketOpener
|
|
seats *buildrun.BuildSeats // the build modules: the holders of the build seats (design 47 G A-82)
|
|
set *buildrun.ToolchainSet
|
|
// put takes a change of a toolchains bucket in: set.Put, or the gate of the assigned build nodes before it (issue 1940).
|
|
put func(module, key string, value []byte, deleted bool) bool
|
|
changed func(ctx context.Context) // a language's entries changed: the failed builds are tried again
|
|
logger *log.Logger
|
|
errs chan error
|
|
mu sync.Mutex
|
|
watched map[string]bool
|
|
}
|
|
|
|
func newToolchainWatch(ctx context.Context, js bucketOpener, seats *buildrun.BuildSeats, set *buildrun.ToolchainSet, changed func(context.Context), logger *log.Logger) *toolchainWatch {
|
|
return &toolchainWatch{ctx: ctx, js: js, seats: seats, set: set, put: set.Put, changed: changed, logger: logger, errs: make(chan error, 16), watched: map[string]bool{}}
|
|
}
|
|
|
|
// start watches the buckets that exist now; a missing one is said and waits for its build module's event.
|
|
func (w *toolchainWatch) start() error {
|
|
for _, module := range w.seats.Modules() {
|
|
if err := w.ensure(module); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *toolchainWatch) ensure(module string) error {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if w.watched[module] {
|
|
return nil
|
|
}
|
|
name := buildrun.ToolchainsBucket(module)
|
|
kv, err := w.js.KeyValue(w.ctx, name)
|
|
switch {
|
|
case errors.Is(err, jetstream.ErrBucketNotFound):
|
|
w.logger.Printf("the bucket %s does not exist yet: it is watched when %s reports a toolchain", name, module)
|
|
return nil
|
|
case err != nil:
|
|
// Refused or not answered (its grant not on the bus yet, issue 1945): said, and opened again at the module's next report.
|
|
w.logger.Printf("the bucket %s cannot be read (%v): %s's toolchains are not heard until its next report", name, err, module)
|
|
return nil
|
|
}
|
|
watcher, err := settle(w.ctx, kv, func(e jetstream.KeyValueEntry) { w.put(module, e.Key(), e.Value(), !opOf(e)) })
|
|
if err != nil {
|
|
process.NotWatched(w.logger.Printf, name, err, fmt.Sprintf("%s's toolchains are not heard until its next report", module))
|
|
return nil
|
|
}
|
|
w.watched[module] = true
|
|
go func() {
|
|
w.errs <- follow(w.ctx, watcher, func(e jetstream.KeyValueEntry) {
|
|
if w.put(module, e.Key(), e.Value(), !opOf(e)) {
|
|
w.changed(w.ctx)
|
|
}
|
|
})
|
|
}()
|
|
w.changed(w.ctx) // what the settle read is new to the builder
|
|
return nil
|
|
}
|
|
|
|
// delivered is the part of a JetStream message the watch uses.
|
|
type delivered interface {
|
|
Subject() string
|
|
Ack() error
|
|
Term() error
|
|
}
|
|
|
|
// Reported takes one toolchain-reported event of a build seat in: the seat is read off the subject,
|
|
// mesh.seat.<seat>.event.toolchain-reported.<node>, and the bucket of its holder is watched if it is not. A subject of no build
|
|
// seat with a holder is terminated; a failure stays unacknowledged, and the server delivers it again.
|
|
func (w *toolchainWatch) Reported(m delivered) {
|
|
seat, _, _ := strings.Cut(strings.TrimPrefix(m.Subject(), "mesh.seat."), ".event.")
|
|
bm, known := w.seats.OfSeat(seat)
|
|
module := bm.Module
|
|
if !known || module == "" || bm.Toolchain == "" || !strings.HasPrefix(m.Subject(), "mesh.seat.") {
|
|
w.logger.Printf("%s: not the event of a build seat with a holder, dropped", m.Subject())
|
|
_ = m.Term()
|
|
return
|
|
}
|
|
if err := w.ensure(module); err != nil {
|
|
w.logger.Printf("%s: left for redelivery: %v", m.Subject(), err)
|
|
return
|
|
}
|
|
_ = m.Ack()
|
|
}
|