The builder leaves an event it cannot take for the bus to give again, and the bus drops it after the fifth delivery its durable allows without a word: the 0.1.6 published was lost so. On that last delivery the builder now says it gives the message up, naming its subject, what it carries (a registry outcome's name and version, a version-released's module and version) and the last refusal. Builds: design 47 G A-420
90 lines
3.1 KiB
Go
90 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
)
|
|
|
|
// The builder's retake of a key failed on a transport fault, on the node-engine seat's events (design 47 G §568, ADR 0673): the
|
|
// wiring of build.Retaker to the events consumer.
|
|
type retaking struct{ rt *build.Retaker }
|
|
|
|
// heard hands a message of the events consumer to the retake: one of its subjects whose state cannot take it is left for the bus to
|
|
// give again (Nak); every message goes on to the other parts.
|
|
func (g *retaking) heard(ctx context.Context, m jetstream.Msg, logf func(string, ...any)) (stop bool) {
|
|
meta, err := m.Metadata()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
handled, err := g.rt.Heard(ctx, m.Subject(), m.Data(), meta.Sequence.Stream)
|
|
if handled && err != nil {
|
|
logf("%v; heard again", err)
|
|
if nakErr := m.Nak(); nakErr != nil {
|
|
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
|
}
|
|
givenUp(m, "its event", err, logf)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The builder's two bus adapters for mesh-sdk's operator-channel asker (design 47 G §568, A-101): its own ask published and its
|
|
// own `decided` heard through the module's hub. The seat's holder and the router's record are the asker's.
|
|
|
|
// publishVia publishes an accept of the seat, de-duplicated by id: the same id twice is one message.
|
|
func publishVia(ctx context.Context, js jetstream.JetStream) func(subject string, body any, id string) error {
|
|
return func(subject string, body any, id string) error {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts []jetstream.PublishOpt
|
|
if id != "" {
|
|
opts = append(opts, jetstream.WithMsgID(id))
|
|
}
|
|
_, err = js.Publish(ctx, subject, data, opts...)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// decisions holds the handler of the router's `decided` event for this module: the module's one hub has the pattern
|
|
// `mesh.seat.*.event.>`, which covers it, so the events handler hands the message here (a hub refuses two patterns that overlap).
|
|
type decisions struct {
|
|
subject string
|
|
handle func(jetstream.Msg)
|
|
}
|
|
|
|
// hear is the client's Subscribe: it keeps the handler of the warrants the router says to this module.
|
|
func (d *decisions) hear(logf func(string, ...any)) func(pattern string, handle func(stdio.Envelope) error) error {
|
|
return func(pattern string, handle func(stdio.Envelope) error) error {
|
|
d.handle = func(m jetstream.Msg) {
|
|
if err := handle(stdio.Envelope{Key: pattern, Body: m.Data()}); err != nil {
|
|
logf("a warrant could not be handled and is given again: %v", err)
|
|
if nakErr := m.Nak(); nakErr != nil {
|
|
logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
|
}
|
|
givenUp(m, "the operator's warrant", err, logf)
|
|
return
|
|
}
|
|
if err := m.Ack(); err != nil {
|
|
logf("a warrant was handled but could not be acknowledged: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// heard takes the message when it is this module's `decided`.
|
|
func (d *decisions) heard(m jetstream.Msg) bool {
|
|
if d.handle == nil || m.Subject() != d.subject {
|
|
return false
|
|
}
|
|
d.handle(m)
|
|
return true
|
|
}
|