The node-engine gives the watcher's process no MESH_NODE, so the watcher refused to start and crash-looped on g14. It needs none: a kept change holds its event, its sequence and its body, and the bundle fills its own node into the subject, the message id <node>.<event>.<sequence> and the body when it publishes, the same at every publish. Builds: design 08 sign-in-page §11, §12, §20; lead answer L-3 (hq 09c9a3e3)
94 lines
3.2 KiB
Go
94 lines
3.2 KiB
Go
package main
|
|
|
|
// The bundle publishes the kept events (design 08 sign-in-page §20): in sequence order, on the seat's subject, each
|
|
// with its message id as the bus's message id (the runtime prefixes it with the module), awaiting the stream's
|
|
// acknowledgement, and deletes each file once acknowledged. A publish refused or unacknowledged stays in pending/, is
|
|
// written to the journal and counted, and is published again at the next moment: the bundle's start, a file event of
|
|
// the kernel on pending/, its bus connection coming back, or uplink_publish_again.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
// publisher publishes the kept events, one round at a time.
|
|
type publisher struct {
|
|
folder Folder
|
|
// publish says one event on the seat, de-duplicated by id, and answers once the stream acknowledged it.
|
|
publish func(subject string, body json.RawMessage, id string) error
|
|
|
|
mu sync.Mutex
|
|
refused int
|
|
}
|
|
|
|
// publishResult is one round: how many were published and how many still wait.
|
|
type publishResult struct {
|
|
Published int `json:"published"`
|
|
Waiting int `json:"waiting"`
|
|
Refused string `json:"refused,omitempty"`
|
|
}
|
|
|
|
// publishKept publishes every kept event in order; the first refused stops the round, so none is published before an
|
|
// older one.
|
|
func (p *publisher) publishKept(moment string) publishResult {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
var out publishResult
|
|
kept, err := p.folder.pending()
|
|
if err != nil {
|
|
journal("reading the kept events at %s failed: %v", moment, err)
|
|
out.Refused = err.Error()
|
|
return out
|
|
}
|
|
for i, k := range kept {
|
|
subject, id, body, err := p.outgoing(k)
|
|
if err == nil {
|
|
err = p.publish(subject, body, id)
|
|
}
|
|
if err != nil {
|
|
p.refused++
|
|
out.Waiting, out.Refused = len(kept)-i, err.Error()
|
|
journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.Name(), moment, err,
|
|
out.Waiting)
|
|
return out
|
|
}
|
|
journal("published %s on %s", id, subject)
|
|
out.Published++
|
|
if err := p.folder.drop(k); err != nil {
|
|
journal("deleting the published %s failed: %v", id, err)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// outgoing is a kept change as the bundle publishes it on its own node: the seat's subject, the message id
|
|
// `<node>.<event>.<sequence>` and the body with its node (§20; lead answer L-3).
|
|
func (p *publisher) outgoing(k Kept) (string, string, json.RawMessage, error) {
|
|
node := p.folder.Node
|
|
if node == "" {
|
|
return "", "", nil, errors.New("no node named: the tool runner names the bundle's in MESH_NODE")
|
|
}
|
|
if k.Event != eventAsked && k.Event != eventEnded {
|
|
return "", "", nil, fmt.Errorf("the kept change %s is of no event of the seat", k.Name())
|
|
}
|
|
var body map[string]json.RawMessage
|
|
if err := json.Unmarshal(k.Body, &body); err != nil {
|
|
return "", "", nil, fmt.Errorf("the kept change %s holds no body: %w", k.Name(), err)
|
|
}
|
|
body["node"], _ = json.Marshal(node)
|
|
raw, err := json.Marshal(body)
|
|
if err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
return eventSubject(k.Event, node), fmt.Sprintf("%s.%s.%d", node, k.Event, k.Sequence), raw, nil
|
|
}
|
|
|
|
// refusedSince is the publishes refused since the bundle's start.
|
|
func (p *publisher) refusedSince() int {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
return p.refused
|
|
}
|