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 mu sync.Mutex missed bool lastAt time.Time } func newBundle(f Folder) *bundle { return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish}} } // 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) } // startBundle is networkmanager's bundle's own work, beside serving its tools. func startBundle() { f, err := theFolder() if err != nil { journal("the module's folder: %v", err) return } b := newBundle(f) b.start(fileEvents, stdio.OnHeartbeat) } // start publishes what is kept, then again at each file event on pending/ and at each return of the bus connection. func (b *bundle) start(files func([]string, <-chan struct{}) <-chan struct{}, onHeartbeat func(func(stdio.Heartbeat)) error) { pending := filepath.Join(b.folder.Dir, pendingName) if err := os.MkdirAll(pending, 0o700); err != nil { journal("making %s failed: %v", pending, err) } kept := files([]string{pending}, nil) b.pub.publishKept("the bundle's start") if err := onHeartbeat(b.heartbeat); err != nil { journal("hearing the bus connection's heartbeat failed: %v", err) } for range kept { b.pub.publishKept("a kept event") } } // 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") }