mesh/delivery merged: source.main moved to novox/mesh-catalog@86fdf74a0a83
mesh/check checking a30c16851246 on 565d0a848a85
Review round 1: offering the work again let a redelivered raise follow its own clearing and replace it, and a redelivery is bounded where an outage is not. The work is now taken; what was shown stays in memory, recalled first, and is kept at the start of the next work and at the minute's standing, stopping at the first write the store does not answer. A desk that restarts before then loses where those are shown, and its log said how many it held.
146 lines
4.2 KiB
Go
146 lines
4.2 KiB
Go
// desk-channel: the desk as a channel of the operator's conversation, kind `desktop` of the `channel` and
|
|
// `intake` benches (novox/hq ADR 0258, ADR 0259). A Go bundle the node's runtime launches. stdout is the MCP
|
|
// channel; what this module says, it says on stderr.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
|
|
|
|
// Worker is the work queue of this kind, as the controller names it.
|
|
const Worker = "SEAT_CHANNEL_DESKTOP_worker"
|
|
|
|
// The notifier's events, which the dunst holder of node-notifier emits as its module's own.
|
|
const (
|
|
AnswerEvent = "dunst.action-chosen"
|
|
StartedEvent = "dunst.started"
|
|
PausedEvent = "dunst.paused"
|
|
)
|
|
|
|
type busStore struct{}
|
|
|
|
func (busStore) Get(handle string) (*Shown, error) {
|
|
e, err := stdio.State("shown").Get(handle)
|
|
if err != nil || e == nil {
|
|
return nil, err
|
|
}
|
|
var s Shown
|
|
return &s, json.Unmarshal(e.Value, &s)
|
|
}
|
|
func (busStore) Put(handle string, s Shown) error {
|
|
_, err := stdio.State("shown").Put(handle, s)
|
|
return err
|
|
}
|
|
func (busStore) Handles() ([]string, error) { return stdio.State("shown").Keys() }
|
|
|
|
// machines reads the setting desktop-machines at each use, so a change takes effect without a restart.
|
|
func machines(path string) []string {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var s struct {
|
|
Machines []string `json:"desktop-machines"`
|
|
}
|
|
if json.Unmarshal(raw, &s) != nil {
|
|
return nil
|
|
}
|
|
var out []string
|
|
for _, m := range s.Machines {
|
|
if m = strings.TrimSpace(m); m != "" {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func main() {
|
|
settings := os.Getenv("MESH_DESK_SETTINGS")
|
|
d := &Desk{
|
|
Machines: func() []string { return machines(settings) },
|
|
Notify: func(machine string, args map[string]any) (json.RawMessage, error) {
|
|
return stdio.Ask("seat:node-notifier.send@"+machine, args)
|
|
},
|
|
Store: busStore{},
|
|
Publish: func(subject string, body any, id string) error { return stdio.SeatPublish(subject, body, id) },
|
|
Logf: logf,
|
|
}
|
|
d.init()
|
|
go run(d)
|
|
tools := []stdio.Tool{{Name: "desk_channel_status",
|
|
Description: "Whether the desk can show the operator's messages, and on which machines: the setting " +
|
|
"desktop-machines, each machine's last answer, and the rules it keeps.",
|
|
Run: func(map[string]any) (any, error) { return d.Status(), nil }}}
|
|
if err := stdio.Serve("", tools); err != nil {
|
|
logf("%v", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(d *Desk) {
|
|
time.Sleep(500 * time.Millisecond) // Serve first: the bus is reached through it
|
|
go func() {
|
|
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
|
if err := stdio.SeatTake(Worker, func(w stdio.Work) error { return d.Take(w.Subject, w.Body) }); err == nil {
|
|
logf("[desk-channel] taking the router's messages for the desk")
|
|
return
|
|
} else {
|
|
logf("[desk-channel] not taking messages yet (%v); asking again in %s", err, wait)
|
|
}
|
|
time.Sleep(wait)
|
|
}
|
|
}()
|
|
go func() {
|
|
for {
|
|
d.KeepUnkept()
|
|
d.SayStanding()
|
|
time.Sleep(time.Minute)
|
|
}
|
|
}()
|
|
handle := func(e stdio.Envelope) error {
|
|
var body struct {
|
|
Token string `json:"token"`
|
|
Machine string `json:"machine"`
|
|
Paused *bool `json:"paused"`
|
|
}
|
|
_ = json.Unmarshal(e.Body, &body)
|
|
// The machine is the event's own node, said by the runtime: the body's word is only a hint.
|
|
machine := e.Node
|
|
if machine == "" {
|
|
machine = body.Machine
|
|
}
|
|
switch e.Key {
|
|
case StartedEvent:
|
|
d.Started(e.Headers["x-event-id"], machine)
|
|
case PausedEvent:
|
|
// Only from the event's own node: a word about another machine is no word at all.
|
|
if body.Paused != nil && e.Node != "" {
|
|
d.Paused(e.Node, *body.Paused)
|
|
d.SayStanding()
|
|
}
|
|
case AnswerEvent:
|
|
if body.Token != "" {
|
|
d.Chosen(e.Headers["x-event-id"], machine, body.Token)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
|
if err := stdio.Subscribe("dunst.*", handle); err == nil {
|
|
logf("[desk-channel] listening for the answers chosen on the desk")
|
|
return
|
|
} else {
|
|
logf("[desk-channel] not hearing the desk's answers yet (%v); asking again in %s", err, wait)
|
|
}
|
|
time.Sleep(wait)
|
|
}
|
|
}
|