node-engine: at its first pass after an update, a process taken as running gets its unit written again with MESH_NODE (1984/AC16, AC17; H-A-711, H-A-713, H-A-714)

A module taken as running its want's composition at the start (the agent's TakenAtStart, now the named
takenAtStartIn the node agent hands it) has the unit of each process whose composed unit differs from its
applied record's written again, by one function (apply.ProcessWrote, which the apply builds its record with
too), and the record takes it, so a second pass finds nothing. Nothing is applied: no backup, no other
resource, the have unchanged. A held module, or one whose want names a new fingerprint, is never taken as
running and is not touched. A process whose record names another bundle, or none, is left as it is and said
(H-A-714); a failed units reload is said.

What restarts: services only. Run-once steps and scheduled processes' units are written, not run or started.
Brother's two processes (brother-device, brother-watch; neither run-once nor scheduled) are services: each
restarts once, if brother runs its want's composition.

Builds: design 47 G A-342, H-A-711, H-A-713, H-A-714
This commit is contained in:
2026-11-25 15:34:13 +01:00
parent c312e6a30e
commit 3c2e60dc1f
7 changed files with 253 additions and 9 deletions
+1 -1
View File
@@ -1629,7 +1629,7 @@ func applyOne(ctx context.Context, sys system.System, r declaration.Resource, ru
if err != nil {
return begin(r), err
}
return applyProcess(ctx, withNode(res, in.node), run, changed, previous, held, in.away)
return applyProcess(ctx, res, in.node, run, changed, previous, held, in.away)
case *declaration.Action:
return applyAction(ctx, res, run)
case *declaration.Network:
+73
View File
@@ -0,0 +1,73 @@
package apply
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/store"
)
// ConvergeProcessUnits writes again the unit of each process of d whose unit as composed (ProcessWrote, with d's node)
// differs from the one its applied record says it wrote, for a module taken as running its want's composition at the
// engine's start (design 47 G A-342, H-A-713): the process then has the node's name in its env. A service is restarted; a
// run-once step's and a scheduled process's unit are written and neither run nor started, and take the name at their next
// run. Nothing else is written or run (no backup, no other resource), and the record takes the new unit, so a second pass
// finds nothing. A process with no record, or whose record holds another bundle than the composition's, is not touched: the
// apply of its new form gives it the name. It answers the changed record state and one line per process.
func ConvergeProcessUnits(ctx context.Context, d *declaration.Declaration, known store.State, run Runner) (store.State, []string) {
var lines []string
if d == nil {
return known, nil
}
for _, res := range d.Resources {
r, ok := res.(*declaration.Process)
if !ok {
continue
}
rec, held := known.Find(r.Identity())
if !held || rec.Type != string(declaration.TypeProcess) {
continue
}
have, same := strings.CutPrefix(rec.Wrote, r.Digest+" ")
want := ProcessWrote(r, d.For)
if !same {
// Another bundle than the composition's (or none) is what the record holds: the apply of the new form gives the
// name (H-A-714). Said, so that the process left as it is can be read from the journal.
named, _, _ := strings.Cut(rec.Wrote, " ")
if named == "" {
named = "none"
}
lines = append(lines, fmt.Sprintf("%s (%s) is left as it is at this start: its record names the bundle %s and the composition names %s",
r.Name, r.ID, named, r.Digest))
continue
}
if have == strings.TrimPrefix(want, r.Digest+" ") {
continue
}
unit := filepath.Join(unitDir, r.Name+".service")
if err := os.WriteFile(unit, []byte(unitFor(withNode(r, d.For))), 0o644); err != nil {
lines = append(lines, fmt.Sprintf("%s (%s) cannot be given its node's name at this start: %v", r.Name, r.ID, err))
continue
}
if _, err := run(ctx, "systemctl", "daemon-reload"); err != nil {
lines = append(lines, fmt.Sprintf("%s (%s) was written with its node's name and the units could not be reloaded: %v", r.Name, r.ID, err))
continue
}
verb := "written again with its node's name at this start"
if !r.RunOnce && r.Schedule == "" {
if _, err := run(ctx, "systemctl", "restart", r.Name+".service"); err != nil {
lines = append(lines, fmt.Sprintf("%s (%s) was written with its node's name and would not restart: %v", r.Name, r.ID, err))
continue
}
verb = "written again with its node's name and restarted at this start"
}
rec.Wrote = want
known.Record(rec)
lines = append(lines, fmt.Sprintf("%s (%s) %s", r.Name, r.ID, verb))
}
return known, lines
}
+10 -2
View File
@@ -42,8 +42,9 @@ var daemonRoot = "/var/lib/mesh/daemons"
// directory of its own.
var unitDir = "/etc/systemd/system"
func applyProcess(ctx context.Context, r *declaration.Process, run Runner,
func applyProcess(ctx context.Context, declared *declaration.Process, node string, run Runner,
changed map[string]bool, previous store.Applied, held []byte, away *storeAway) (Outcome, error) {
r := withNode(declared, node)
out := begin(r)
out.Action = "unchanged"
@@ -67,7 +68,7 @@ func applyProcess(ctx context.Context, r *declaration.Process, run Runner,
// **Its identity is its bytes AND how it is run.** Two processes from one bundle differing
// only in their command are different, and a record tracking the digest alone would call the
// second one unchanged.
want := got + " " + unitFor(r)
want := ProcessWrote(declared, node)
at := filepath.Join(daemonRoot, r.Name)
// **Something it reads changed, so it must be restarted even though it is unchanged.** A
@@ -293,6 +294,13 @@ func withNode(r *declaration.Process, node string) *declaration.Process {
return &p
}
// ProcessWrote is what the engine's record holds as written for a process on node: its bundle's digest and the unit it
// writes for it, with the node's name in its env (design 47 G A-342). The apply builds its record with it, and the start's
// comparison reads it (H-A-713, one function).
func ProcessWrote(r *declaration.Process, node string) string {
return r.Digest + " " + unitFor(withNode(r, node))
}
// drainStopMargin is the seconds a draining process's stop timeout lies above its drain bound: the time it takes to stop
// its children after its drain (H-A-358).
const drainStopMargin = 15