193 lines
7.0 KiB
Go
193 lines
7.0 KiB
Go
package grants
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
"git.novox.be/novox/mesh-sdk/go/membership"
|
|
|
|
"mesh-grants/internal/buscompose"
|
|
)
|
|
|
|
// **The purge of the build seats' named asks** (design 47 G §544, A-231): at the first revision in which no entry invokes any
|
|
// `seat:<seat>.accept.<queue>`, the part purges the subjects `accept.<queue>.*` of each build seat's stream and deletes the
|
|
// named workers, within its pass, saying each in its journal; where that did not run (the part stopped first), the tool
|
|
// grants_named_purge, written in the hand-act log first, asks the part's process to do it by hand. An ask is transport, not
|
|
// kept data.
|
|
|
|
// ToolNamedPurge is the hand tool that purges the named asks and deletes the named workers.
|
|
const ToolNamedPurge = "grants_named_purge"
|
|
|
|
// NamedPurger purges one subject of a stream and deletes one consumer: the process's connection; nil in the tools.
|
|
type NamedPurger interface {
|
|
PurgeSubject(stream, subject string) error
|
|
// DeleteConsumer deletes a consumer; one the bus does not hold is no error.
|
|
DeleteConsumer(stream, name string) error
|
|
}
|
|
|
|
// NamedAsksUnused says the revision the part composes from holds entries and none invokes a queue of a build seat
|
|
// (catalogue.QueueInvoke); false while the catalog half is not read, so a failed read purges nothing.
|
|
func NamedAsksUnused(in Inputs) bool {
|
|
if in.CatalogUnread != "" || len(in.Manifests) == 0 || in.CarriedNamedAsks {
|
|
return false
|
|
}
|
|
for _, m := range in.Manifests {
|
|
for _, i := range m.Invokes {
|
|
if _, _, ok := catalogue.QueueInvoke(i); ok {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
// The manifest each machine is wanted to run counts as an entry of the revision too: a build taken from its want's newest
|
|
// build (A-414) that invokes a queue keeps the named workers (A-413).
|
|
for _, m := range in.Running {
|
|
for _, i := range m.Invokes {
|
|
if _, _, ok := catalogue.QueueInvoke(i); ok {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// namedAsksWhy says what decided whether the named asks are unused, for the journal.
|
|
func namedAsksWhy(in Inputs, granted bool) string {
|
|
switch {
|
|
case in.CatalogUnread != "":
|
|
return "the catalog half is unread"
|
|
case len(in.Manifests) == 0:
|
|
return "no manifest is held"
|
|
case in.CarriedNamedAsks:
|
|
return "a carried membership is granted to publish a named ask"
|
|
case granted:
|
|
return "a composed account is granted to publish a named ask"
|
|
case NamedAsksUnused(in):
|
|
return "no entry of the revision and no wanted build invokes a build seat's queue"
|
|
}
|
|
return "an entry of the revision or a wanted build invokes a build seat's queue"
|
|
}
|
|
|
|
// withNoNamedAsks marks each pulled-by-kind seat a declaration holds as having no named worker while unused.
|
|
func withNoNamedAsks(unused bool, d *buscompose.Declared) {
|
|
for _, list := range []*[]buscompose.Seat{&d.Holds, &d.Uses, &d.Watches} {
|
|
for i := range *list {
|
|
if (*list)[i].PulledByKind {
|
|
(*list)[i].NoNamedAsks = unused
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// purgeNamed purges the named asks and deletes the named workers of every build seat's queue the records compose, at the
|
|
// first pass whose revision leaves them unused, and on a person's ask whatever the revision says.
|
|
func (p *Part) purgeNamed(res *Result, in Inputs, recs buscompose.Records, byHand string) {
|
|
unused := NamedAsksUnused(in) && !buscompose.NamedAsksGranted(recs)
|
|
due := unused && !p.namedPurged || byHand != ""
|
|
p.namedPurged = unused
|
|
if !due || p.NamedPurge == nil {
|
|
return
|
|
}
|
|
users, err := buscompose.Users(recs)
|
|
if err != nil {
|
|
res.Errors = append(res.Errors, "the named asks could not be purged: "+err.Error())
|
|
return
|
|
}
|
|
why := "no entry of the revision invokes a queue of a build seat"
|
|
if byHand != "" {
|
|
why = "by hand, for " + byHand
|
|
}
|
|
for _, q := range buscompose.NamedQueuesOf(users) {
|
|
if err := p.NamedPurge.PurgeSubject(q.Stream, q.Subject); err != nil {
|
|
res.Errors = append(res.Errors, fmt.Sprintf("the named asks %s of %s were not purged: %v", q.Subject, q.Stream, err))
|
|
p.namedPurged = false
|
|
} else {
|
|
res.NamedPurged = append(res.NamedPurged, q.Stream+" "+q.Subject)
|
|
p.logf("purged the named asks %s of %s (%s)", q.Subject, q.Stream, why)
|
|
}
|
|
if err := p.NamedPurge.DeleteConsumer(q.Stream, q.Worker); err != nil {
|
|
res.Errors = append(res.Errors, fmt.Sprintf("the named worker %s.%s was not deleted: %v", q.Stream, q.Worker, err))
|
|
p.namedPurged = false
|
|
} else {
|
|
res.NamedPurged = append(res.NamedPurged, q.Stream+"."+q.Worker)
|
|
p.logf("deleted the named worker %s.%s (%s)", q.Stream, q.Worker, why)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *Part) logf(format string, args ...any) {
|
|
if p.Logf != nil {
|
|
p.Logf(format, args...)
|
|
}
|
|
}
|
|
|
|
// AskNamedPurge answers the tool grants_named_purge: it records the hand act through record (the seat mesh-hand-acts) and only
|
|
// then asks the part's process to purge the named asks and delete the named workers of every build seat's queue.
|
|
func (p *Part) AskNamedPurge(caller, why string, record func(what, why string) error) (any, error) {
|
|
if strings.TrimSpace(why) == "" {
|
|
return nil, errors.New(ToolNamedPurge + " needs why: it is recorded as a hand act")
|
|
}
|
|
if record == nil {
|
|
return nil, errors.New("the hand act cannot be recorded here, so nothing was asked")
|
|
}
|
|
if err := record(ToolNamedPurge, why); err != nil {
|
|
return nil, fmt.Errorf("the hand act could not be recorded, so nothing was asked: %w", err)
|
|
}
|
|
by := caller
|
|
if by == "" {
|
|
by = "a person"
|
|
}
|
|
ask, _ := json.Marshal(Ask{By: by, At: p.now(), NamedPurge: true})
|
|
rev, err := p.Store.Put(StateUserList, KeyAsked, ask)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the ask could not be written: %w", err)
|
|
}
|
|
return map[string]any{"asked": rev, "says": "the part's process purges the named asks and deletes the named workers at this ask; its journal names each"}, nil
|
|
}
|
|
|
|
// NamedAskPublish says a seat-traffic publish subject is a named ask of a queue, `mesh.seat.<seat>.accept.<queue>.<asker>`: five
|
|
// or more tokens with `accept` the fourth; main's `accept.<queue>` has four.
|
|
func NamedAskPublish(subject string) bool {
|
|
t := strings.Split(subject, ".")
|
|
return len(t) >= 6 && t[0] == "mesh" && t[1] == "seat" && t[3] == "accept"
|
|
}
|
|
|
|
// withCarriedNamedAsks reads the memberships the part holds in its own state, which it publishes (composed in this pass or
|
|
// carried over for a module it cannot compose) and sets Inputs.CarriedNamedAsks where one grants a named ask (A-413). An
|
|
// unreadable state sets it, so a failed read purges nothing.
|
|
func (p *Part) withCarriedNamedAsks(in *Inputs) {
|
|
if p.Store == nil {
|
|
return
|
|
}
|
|
keys, err := p.Store.Keys(StateMemberships)
|
|
if err != nil {
|
|
in.CarriedNamedAsks = true
|
|
return
|
|
}
|
|
for _, k := range keys {
|
|
raw, _, found, err := p.Store.Get(StateMemberships, k)
|
|
if err != nil {
|
|
in.CarriedNamedAsks = true
|
|
return
|
|
}
|
|
if !found {
|
|
continue
|
|
}
|
|
bare, err := membership.BareGrantsMembership(raw)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var m membership.Membership
|
|
if json.Unmarshal(bare, &m) != nil || m.SeatTraffic == nil {
|
|
continue
|
|
}
|
|
if slices.ContainsFunc(m.SeatTraffic.Publish, NamedAskPublish) {
|
|
in.CarriedNamedAsks = true
|
|
return
|
|
}
|
|
}
|
|
}
|