G A-98: the node agent gains Env.Stop, which stops a module's units and containers and removes nothing
Builds: design 47 G §297, A-98 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
This commit is contained in:
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/novox/mesh-host/internal/seatwait"
|
||||
"github.com/novox/mesh-host/internal/selfcompose"
|
||||
"github.com/novox/mesh-host/internal/store"
|
||||
"github.com/novox/mesh-host/internal/system"
|
||||
)
|
||||
|
||||
// The node agent's wiring (novox/hq ADR 0429 §4, ADR 0539 §9): the buckets it watches, the haves it writes, the backup
|
||||
@@ -578,6 +579,9 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus
|
||||
sayFacts(ctx, ag)
|
||||
return res
|
||||
},
|
||||
Stop: func(ctx context.Context, module string) agent.Result {
|
||||
return stopComposition(ctx, opts, module)
|
||||
},
|
||||
Remove: func(ctx context.Context, module string, others []compositions.Composed) agent.Result {
|
||||
actx, done := applyContext(session.WithLink(ctx))
|
||||
res := removeComposition(actx, opts, module, others, ag.Placed(module), sched, say)
|
||||
@@ -857,6 +861,30 @@ func removeComposition(ctx context.Context, opts options, module string, others
|
||||
return agent.Result{}
|
||||
}
|
||||
|
||||
// stopComposition stops a module's units and containers from the node's record and removes nothing (design 47 G A-98,
|
||||
// §297): what the module kept stays, so a revert restores it before the reverted build starts.
|
||||
func stopComposition(ctx context.Context, opts options, module string) agent.Result {
|
||||
applying.Lock()
|
||||
defer applying.Unlock()
|
||||
unlock, err := store.Lock(opts.state, nil)
|
||||
if err != nil {
|
||||
return agent.Result{Failed: true, Detail: shorten(err.Error())}
|
||||
}
|
||||
defer unlock()
|
||||
known, err := store.Load(opts.state)
|
||||
if err != nil {
|
||||
return agent.Result{Failed: true, Detail: shorten(err.Error())}
|
||||
}
|
||||
built, err := system.For(builtFor)
|
||||
if err != nil {
|
||||
return agent.Result{Failed: true, Detail: shorten(err.Error())}
|
||||
}
|
||||
if _, err := apply.StopModule(ctx, built, known, module, opts.run()); err != nil {
|
||||
return agent.Result{Failed: true, Detail: shorten(err.Error())}
|
||||
}
|
||||
return agent.Result{}
|
||||
}
|
||||
|
||||
// busyModules are the modules whose last apply left a container running because it said busy.
|
||||
func busyModules() []string {
|
||||
busyMu.Lock()
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/novox/mesh-host/internal/store"
|
||||
)
|
||||
|
||||
// The node agent's Stop stops a module's container from the node's record, asks the machine nothing else and leaves the
|
||||
// record, so the module's kept data and its record survive (design 47 G A-98).
|
||||
func TestTheNodeAgentsStopStopsAModuleAndRemovesNothing(t *testing.T) {
|
||||
was := builtFor
|
||||
builtFor = "arch"
|
||||
t.Cleanup(func() { builtFor = was })
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
var s store.State
|
||||
s.Record(store.Applied{ID: "app.box", Type: "container", Target: "app-box"})
|
||||
s.Record(store.Applied{ID: "other.box", Type: "container", Target: "other-box"})
|
||||
if err := store.Save(path, s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var asked []string
|
||||
opts := options{state: path, runner: func(_ context.Context, name string, args ...string) (string, error) {
|
||||
asked = append(asked, name+" "+strings.Join(args, " "))
|
||||
return "", nil
|
||||
}}
|
||||
if res := stopComposition(context.Background(), opts, "app"); res.Failed {
|
||||
t.Fatalf("stop failed: %s", res.Detail)
|
||||
}
|
||||
if !strings.Contains(strings.Join(asked, "\n"), "stop app-box") || strings.Contains(strings.Join(asked, "\n"), "other-box") ||
|
||||
strings.Contains(strings.Join(asked, "\n"), " rm ") {
|
||||
t.Fatalf("the machine was asked %v", asked)
|
||||
}
|
||||
if after, err := store.Load(path); err != nil || len(after.Resources) != 2 {
|
||||
t.Fatalf("the record changed: %v, %v", after.Resources, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user