The bundle serves uplink_sign_in, uplink_names_again, uplink_rating_again, uplink_publish_again and uplink_health: the read of the reaction, the three acts the parts do by themselves, and the watcher process's health, which also gives the names again where the resolver no longer holds them. Builds: design 08 sign-in-page §13, §23
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package main
|
|
|
|
// The bundle's own work beside the seat's verbs (design 08 sign-in-page §11, §13, §20): started by the tool runner
|
|
// once it reached the bus. It acts at its start, at file events of the kernel in the module's folder, and when its bus
|
|
// connection comes back: the first heartbeat of its consumer answered after a missed one (DMC-4).
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
type bundle struct {
|
|
folder Folder
|
|
pub *publisher
|
|
names *namer
|
|
|
|
mu sync.Mutex
|
|
missed bool
|
|
lastAt time.Time
|
|
}
|
|
|
|
// bundleRuns is what the bundle hears through: file events of the kernel, its consumer's heartbeat and its
|
|
// subscription.
|
|
type bundleRuns struct {
|
|
files func(dirs []string, stop <-chan struct{}) <-chan struct{}
|
|
fileMade func(dir, name string, stop <-chan struct{}) <-chan struct{}
|
|
onHeartbeat func(func(stdio.Heartbeat)) error
|
|
subscribe func(pattern string, handle func(stdio.Envelope) error) error
|
|
}
|
|
|
|
func newBundle(f Folder) *bundle {
|
|
return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish},
|
|
names: &namer{folder: f, ask: stdio.Ask, now: time.Now}}
|
|
}
|
|
|
|
// seatPublish says a kept event through the runtime, which awaits the stream's acknowledgement.
|
|
func seatPublish(subject string, body json.RawMessage, id string) error {
|
|
return stdio.SeatPublish(subject, body, id)
|
|
}
|
|
|
|
// start publishes what is kept and gives the resolver the check's host name, then does each again at its moments: a
|
|
// file event on pending/ publishes, a changed check-address gives the names, node-resolver's holder running gives the
|
|
// names, and the bus connection's return does both (§13, §20).
|
|
func (b *bundle) start(runs bundleRuns) {
|
|
pending := filepath.Join(b.folder.Dir, pendingName)
|
|
if err := os.MkdirAll(pending, 0o700); err != nil {
|
|
journal("making %s failed: %v", pending, err)
|
|
}
|
|
kept := runs.files([]string{pending}, nil)
|
|
check := runs.fileMade(b.folder.Dir, checkName, nil)
|
|
b.pub.publishKept("the bundle's start")
|
|
b.names.give("the bundle's start")
|
|
if err := runs.subscribe(runningPattern, b.names.running); err != nil {
|
|
journal("subscribing to %s failed: %v", runningPattern, err)
|
|
}
|
|
if err := runs.onHeartbeat(b.heartbeat); err != nil {
|
|
journal("hearing the bus connection's heartbeat failed: %v", err)
|
|
}
|
|
for kept != nil || check != nil {
|
|
select {
|
|
case _, ok := <-kept:
|
|
if !ok {
|
|
kept = nil
|
|
continue
|
|
}
|
|
b.pub.publishKept("a kept event")
|
|
case _, ok := <-check:
|
|
if !ok {
|
|
check = nil
|
|
continue
|
|
}
|
|
b.names.give("a changed check address")
|
|
}
|
|
}
|
|
}
|
|
|
|
// heartbeat hears the consumer's heartbeat: the first one answered after a missed one is the connection come back.
|
|
func (b *bundle) heartbeat(hb stdio.Heartbeat) {
|
|
b.mu.Lock()
|
|
if !hb.At.IsZero() && hb.At.Before(b.lastAt) {
|
|
b.mu.Unlock()
|
|
return
|
|
}
|
|
if !hb.At.IsZero() {
|
|
b.lastAt = hb.At
|
|
}
|
|
back := !hb.Missed && b.missed
|
|
b.missed = hb.Missed
|
|
b.mu.Unlock()
|
|
if hb.Missed {
|
|
journal("the bus connection's heartbeat was missed: %s", hb.Why)
|
|
}
|
|
if back {
|
|
journal("the bus connection came back")
|
|
b.reconnected()
|
|
}
|
|
}
|
|
|
|
// reconnected is the work at the bus connection's return.
|
|
func (b *bundle) reconnected() {
|
|
b.pub.publishKept("the bus connection's return")
|
|
b.names.give("the bus connection's return")
|
|
}
|