JetStreamAsks.WatchAsk opens the watch as a kept call and takes a closed watch up after the last revision heard; the first fault arms a key of the state bounds with its own time to live (StepsBoundKey), whose end while the read is unanswered raises call-unanswered about the ask, and the answer ends it. Builds: design 47 G §568, A-77 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// **The builder's read of an ask's steps is a kept call, unanswered past five minutes a finding** (design 47 G §568, A-77, ADR
|
|
// 0668 §3): a transport fault opening or taking up the watch of `steps` ends no build key; the read is made again once the bus
|
|
// answers. The first fault arms a key of the state bounds (StepsBoundKey) with its own time to live; the bus's marker of its end,
|
|
// heard on the watch of the state, raises call-unanswered about the ask while the read is still unanswered; the answer deletes
|
|
// the key and clears the finding. No timer.
|
|
type stepsRead struct {
|
|
bounds func() jetstream.KeyValue
|
|
findings *findings
|
|
bound time.Duration
|
|
now func() time.Time
|
|
logf func(string, ...any)
|
|
|
|
mu sync.Mutex
|
|
open map[string]string // the asks whose read is unanswered, by ask id: the last fault
|
|
}
|
|
|
|
// unanswered is told each fault of the read of the ask's steps on module: the first arms the bound.
|
|
func (s *stepsRead) unanswered(module, askID string, fault error) {
|
|
s.mu.Lock()
|
|
if s.open == nil {
|
|
s.open = map[string]string{}
|
|
}
|
|
_, armed := s.open[askID]
|
|
s.open[askID] = fault.Error()
|
|
s.mu.Unlock()
|
|
if armed {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
var err error
|
|
if kv := s.bounds(); kv == nil {
|
|
err = fmt.Errorf("%s is not on the bus yet", versions.BoundsBucket)
|
|
} else if _, err = kv.Create(ctx, versions.StepsBoundKey(module, askID), versions.EncodeBound("the read of the steps of the ask "+askID+" on "+module, s.now().Add(s.bound)), jetstream.KeyTTL(s.bound)); errors.Is(err, jetstream.ErrKeyExists) {
|
|
err = nil
|
|
}
|
|
if err != nil {
|
|
s.logf("the bound of the read of the steps of the ask %s is not written, so it is raised now: %v", askID, err)
|
|
s.raise(ctx, module, askID, fault.Error(), s.now())
|
|
}
|
|
}
|
|
|
|
// answered ends the read's bound and clears its finding.
|
|
func (s *stepsRead) answered(module, askID string) {
|
|
s.mu.Lock()
|
|
_, was := s.open[askID]
|
|
delete(s.open, askID)
|
|
s.mu.Unlock()
|
|
if !was {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
if kv := s.bounds(); kv != nil {
|
|
if err := kv.Delete(ctx, versions.StepsBoundKey(module, askID)); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
s.logf("the bound of the read of the steps of the ask %s is not deleted: %v", askID, err)
|
|
}
|
|
}
|
|
s.findings.clear(ctx, meshconditionsclient.KindCallUnanswered, meshconditionsclient.ScopeCall, askID)
|
|
}
|
|
|
|
// expired is told each change of the state bounds: the end of a steps bound raises call-unanswered while its read is unanswered.
|
|
func (s *stepsRead) expired(ctx context.Context, e jetstream.KeyValueEntry) {
|
|
module, askID, ok := versions.StepsBoundOf(e.Key())
|
|
if !ok || e.Operation() != jetstream.KeyValuePurge {
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
fault, open := s.open[askID]
|
|
s.mu.Unlock()
|
|
if !open {
|
|
return
|
|
}
|
|
s.raise(ctx, module, askID, fault, s.now().Add(-s.bound))
|
|
}
|
|
|
|
func (s *stepsRead) raise(ctx context.Context, module, askID, fault string, since time.Time) {
|
|
o := callUnanswered(askID, "the read of the steps of "+module, "", fault, "builder."+meshconditionsclient.KindCallUnanswered, since)
|
|
o.needs = "the bus answers the read again by itself; builder_stuck shows the key waiting on the ask."
|
|
s.findings.raise(ctx, o)
|
|
}
|