Files
mesh-host/internal/apply/converge_units.go
T
jschoubben 3c2e60dc1f 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
2026-11-25 15:34:13 +01:00

74 lines
3.0 KiB
Go

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
}