The builder says wait-held for a key queued behind a package, the package as its part and on (package:<ref>), on its stream waits, and wait-ended published once the key is claimed; a toolchain wait never ends by that, and a package wait never by a toolchain's end. Files: internal/build/waits.go (Package, On, WaitPublished), internal/build/run.go (Hold at the queued result, End at the claim), cmd/mesh-delivery-builder/waits.go (Part in the body, the End rule). Builds: design 47 G A-192, §499 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
134 lines
4.4 KiB
Go
134 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
)
|
|
|
|
// waitBody is the body of wait-held and wait-ended on the builder's stream waits (design 47 G §672): the fields of §629's
|
|
// wait with `step` building, `on` toolchain:<language>@<version>, `module` and `fingerprint`, and, on wait-ended, `how`.
|
|
type waitBody struct {
|
|
Step string `json:"step"`
|
|
On string `json:"on"`
|
|
// Part is the package a key queued behind it waits for (design 47 G A-192); none for a toolchain wait.
|
|
Part string `json:"part,omitempty"`
|
|
Module string `json:"module"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
Since time.Time `json:"since"`
|
|
Bound string `json:"bound,omitempty"`
|
|
How string `json:"how,omitempty"`
|
|
}
|
|
|
|
const waitStepBuilding = "building"
|
|
|
|
// waitsKept keeps the builder's toolchain waits in its old state waits, one key <module>.<fingerprint> (design 47 G §672),
|
|
// and says wait-held and wait-ended on its old stream waits; at its start it says wait-held again for every key.
|
|
type waitsKept struct {
|
|
kv *process.AwaitedBucket
|
|
say func(ctx context.Context, event, id string, body any) error
|
|
bound time.Duration
|
|
logf func(string, ...any)
|
|
|
|
mu sync.Mutex
|
|
held map[string]build.ToolchainWait
|
|
}
|
|
|
|
func newWaitsKept(kv *process.AwaitedBucket, say func(context.Context, string, string, any) error, bound time.Duration, logf func(string, ...any)) *waitsKept {
|
|
return &waitsKept{kv: kv, say: say, bound: bound, logf: logf, held: map[string]build.ToolchainWait{}}
|
|
}
|
|
|
|
func (w *waitsKept) body(t build.ToolchainWait, how string) waitBody {
|
|
return waitBody{Step: waitStepBuilding, On: t.On(), Part: t.Package, Module: t.Module, Fingerprint: t.Fingerprint, Since: t.Since, Bound: w.bound.String(), How: how}
|
|
}
|
|
|
|
// start reads the state whole and says wait-held again for each key.
|
|
func (w *waitsKept) start(ctx context.Context, kv jetstream.KeyValue) {
|
|
keys, err := kv.Keys(ctx)
|
|
if err != nil && !errors.Is(err, jetstream.ErrNoKeysFound) {
|
|
w.logf("%s cannot be read, so the waits of before this start are not said again: %v", build.WaitsState, err)
|
|
return
|
|
}
|
|
for _, k := range keys {
|
|
e, err := kv.Get(ctx, k)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var t build.ToolchainWait
|
|
if json.Unmarshal(e.Value(), &t) != nil || t.Module == "" {
|
|
w.logf("%s.%s holds what is not a wait, and is left", build.WaitsState, k)
|
|
continue
|
|
}
|
|
w.mu.Lock()
|
|
w.held[k] = t
|
|
w.mu.Unlock()
|
|
w.said(ctx, "wait-held", t, "")
|
|
}
|
|
}
|
|
|
|
func (w *waitsKept) said(ctx context.Context, event string, t build.ToolchainWait, how string) {
|
|
if err := w.say(ctx, event+"."+t.Module+"."+t.Fingerprint, event+"-"+t.Module+"-"+t.Fingerprint+"-"+t.Since.UTC().Format(time.RFC3339Nano)+how, w.body(t, how)); err != nil {
|
|
w.logf("saying %s of %s at %s: %v", event, t.Module, t.Fingerprint, err)
|
|
}
|
|
}
|
|
|
|
// Hold keeps the wait and says wait-held, once.
|
|
func (w *waitsKept) Hold(ctx context.Context, t build.ToolchainWait) {
|
|
key, err := builds.BuilderBuildKey(t.Module, t.Fingerprint)
|
|
if err != nil {
|
|
w.logf("a wait of %s at %s has no key: %v", t.Module, t.Fingerprint, err)
|
|
return
|
|
}
|
|
w.mu.Lock()
|
|
if _, had := w.held[key]; had {
|
|
w.mu.Unlock()
|
|
return
|
|
}
|
|
w.held[key] = t
|
|
w.mu.Unlock()
|
|
if kv := w.kv.KV(); kv != nil {
|
|
data, _ := json.Marshal(t)
|
|
if _, err := kv.Put(ctx, key, data); err != nil {
|
|
w.logf("%s.%s could not be written, so the wait is kept in memory only: %v", build.WaitsState, key, err)
|
|
}
|
|
}
|
|
w.said(ctx, "wait-held", t, "")
|
|
}
|
|
|
|
// End deletes the wait and says wait-ended with how; it answers whether one was held.
|
|
func (w *waitsKept) End(ctx context.Context, module, fp, how string) bool {
|
|
key, err := builds.BuilderBuildKey(module, fp)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
w.mu.Lock()
|
|
t, had := w.held[key]
|
|
// A key queued behind a package ends only when it is published, and a toolchain wait never by that (design 47 G A-192).
|
|
if had && (t.Package != "") != (how == build.WaitPublished) {
|
|
had = false
|
|
}
|
|
if had {
|
|
delete(w.held, key)
|
|
}
|
|
w.mu.Unlock()
|
|
if !had {
|
|
return false
|
|
}
|
|
if kv := w.kv.KV(); kv != nil {
|
|
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
w.logf("%s.%s could not be deleted: %v", build.WaitsState, key, err)
|
|
}
|
|
}
|
|
w.said(ctx, "wait-ended", t, how)
|
|
return true
|
|
}
|