diff --git a/go/process/event_hub.go b/go/process/event_hub.go index dd37a5b8..0af4a850 100644 --- a/go/process/event_hub.go +++ b/go/process/event_hub.go @@ -8,6 +8,8 @@ import ( "time" "github.com/nats-io/nats.go/jetstream" + + "git.novox.be/novox/mesh-sdk/go/seatdef" ) // EventHub is the one pull of a module's durable consumer on the shared events stream (novox/hq ADR 0635 ยง5): a module @@ -24,6 +26,8 @@ type EventHub struct { routes []hubRoute cons jetstream.ConsumeContext bound []func(created time.Time) + // kept are the module's durables on the kept seat events' streams it handles (StartKept). + kept []jetstream.ConsumeContext } type hubRoute struct { @@ -152,6 +156,74 @@ func (h *EventHub) Stop() { h.cons.Stop() h.cons = nil } + for _, cc := range h.kept { + cc.Stop() + } + h.kept = nil +} + +// StartKept binds, beside EVENTS, the module's durable on each kept seat event's stream (`SEAT__`) whose event a +// registered pattern takes: a kept event is never on EVENTS, and the tool runner reads its own twin of that durable (lead +// answer A-397), so the program hears it only here. Each stream bound is said, and each it cannot bind (no durable composed +// for this module there, or the bus refuses) is said and passed over. It returns how many it bound. +func (h *EventHub) StartKept(ctx context.Context, js jetstream.JetStream, cred Account) int { + h.mu.RLock() + var patterns []string + for _, r := range h.routes { + patterns = append(patterns, r.pattern) + } + h.mu.RUnlock() + names := js.StreamNames(ctx) + var streams []string + for name := range names.Name() { + streams = append(streams, name) + } + if err := names.Err(); err != nil { + h.logf("events: the kept seat events' streams are not listed (%v): none of them is heard", err) + } + bound := 0 + for _, stream := range streams { + seat, event, ok := keptSeatEvent(stream) + if !ok { + continue + } + subject := "mesh.seat." + seat + ".event." + event + taken := false + for _, p := range patterns { + if subjectMatches(p, subject) || subjectMatches(p, subject+".x") { + taken = true + break + } + } + if !taken { + continue + } + cc, err := cred.HearSeatEvents(ctx, js, stream, h.hear, h.logf) + if err != nil { + h.logf("events: the kept event %s.%s is not heard on %s: %v", seat, event, stream, err) + continue + } + h.mu.Lock() + h.kept = append(h.kept, cc) + h.mu.Unlock() + bound++ + h.logf("events: the consumer %s on %s is heard (the kept event %s.%s)", cred.EventConsumerName(), stream, seat, event) + } + return bound +} + +// keptSeatEvent is the seat and the event a kept seat event's stream carries, from its name `SEAT__` (seatdef.KeptStream): +// a seat's and an event's names hold no underscore. +func keptSeatEvent(stream string) (seat, event string, ok bool) { + rest, isKept := strings.CutPrefix(stream, seatdef.KeptStreamPrefix) + if !isKept { + return "", "", false + } + seat, event, ok = strings.Cut(rest, "_") + if !ok || seat == "" || event == "" || strings.Contains(event, "_") { + return "", "", false + } + return seat, event, true } func (h *EventHub) hear(m jetstream.Msg) { diff --git a/go/process/keptstart_test.go b/go/process/keptstart_test.go new file mode 100644 index 00000000..1e74dd03 --- /dev/null +++ b/go/process/keptstart_test.go @@ -0,0 +1,18 @@ +package process + +import "testing" + +// A-397: a kept seat event's stream names its seat and event; a work queue's stream and any other name are none. +func TestKeptSeatEventOfAStreamName(t *testing.T) { + for _, c := range []struct{ stream, seat, event string }{ + {"SEAT_node-build-go_toolchain-reported", "node-build-go", "toolchain-reported"}, + {"SEAT_mesh-catalog_versions-filled", "mesh-catalog", "versions-filled"}, + {"SEAT_NODE_BUILD_GO", "", ""}, + {"EVENTS", "", ""}, + } { + seat, event, ok := keptSeatEvent(c.stream) + if ok != (c.seat != "") || seat != c.seat || event != c.event { + t.Errorf("%s: %q %q %v, want %q %q", c.stream, seat, event, ok, c.seat, c.event) + } + } +}