Files
mesh-host/internal/apply/stop.go
T
jschoubben 6d866665c2 H2 owner-pass-with-units-stopped: the owner pass at the engine's start starts again exactly what its stop found running
StopModule answers only what it found running (a container found stopped is not in it); StartModule starts that answer, services before timers, then containers.

Builds: design 47 H2 H-A-752 (amends H-A-735 (1)), H-A-641
2026-11-25 18:52:01 +01:00

130 lines
5.0 KiB
Go

package apply
import (
"context"
"fmt"
"sort"
"strings"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/compose/resolution"
"github.com/novox/mesh-host/internal/store"
"github.com/novox/mesh-host/internal/system"
)
// unitsOf is the timers, services and containers the node's record holds for a module, its processes' units among them but
// never the tool runner's own (H-A-641).
func unitsOf(known store.State, module string) (timers, services, containers []store.Applied) {
for _, r := range known.Resources {
if store.OwnerOf(r, declaration.ModuleGuess) != module {
continue
}
switch r.Type {
case string(declaration.TypeService):
if r.Stateless {
continue
}
if strings.HasSuffix(r.Target, ".timer") {
timers = append(timers, r)
} else {
services = append(services, r)
}
case string(declaration.TypeContainer):
containers = append(containers, r)
case string(declaration.TypeProcess):
// The tool runner is never stopped, whichever module's owner pass asks (H-A-641).
if strings.HasSuffix(r.ID, "."+resolution.RuntimeProcessID()) {
continue
}
timers = append(timers, store.Applied{ID: r.ID, Type: string(declaration.TypeService), Target: r.Target + ".timer"})
services = append(services, store.Applied{ID: r.ID, Type: string(declaration.TypeService), Target: r.Target + ".service"})
}
}
return timers, services, containers
}
// StartModule starts again exactly what StopModule found running and stopped, which is what it is given as stopped: the
// module's services, then its timers, then its containers, and nothing else (design 47 H2, H-A-752: a pass at the engine's
// start, with no apply after it, starts again the units it stopped, never a run-once step's service, a scheduled
// process's service off its timer or a unit found stopped before). It answers what it started, and the first failure.
func StartModule(ctx context.Context, sys system.System, known store.State, module string, stopped []string, run Runner) ([]string, error) {
timers, services, containers := unitsOf(known, module)
was := map[string]bool{}
for _, name := range stopped {
was[name] = true
}
var started []string
for _, r := range append(services, timers...) {
if !was[r.Target] {
continue
}
manager := managerFor(r.Scope, r.User, run)
if err := sys.SetServiceState(ctx, manager, r.Target, "running"); err != nil {
return started, fmt.Errorf("starting %s of %s: %w", r.Target, module, err)
}
started = append(started, r.Target)
}
var cri string
for _, r := range containers {
if !was[r.Target] {
continue
}
if cri == "" {
var err error
if cri, err = containerRuntime(ctx, run); err != nil {
return started, err
}
}
if _, err := run(ctx, cri, "start", r.Target); err != nil {
return started, fmt.Errorf("starting the container %s of %s: %w", r.Target, module, err)
}
started = append(started, r.Target)
}
return started, nil
}
// StopModule stops the units and containers the node's record holds for a module and removes nothing: no unit, file,
// container, data or record, so what the module kept survives and a later Apply starts the build it is given (design 47
// G A-98, §297). Units come first, a timer before the service it starts, then containers; a unit the mesh never held
// (Stateless) is left as it is. The module's processes are stopped with them (design 47 H2, H-A-641): the timer and the
// service of each, but never the tool runner's own process, which serves every other module's bundles. It answers what it
// stopped, and the first failure.
func StopModule(ctx context.Context, sys system.System, known store.State, module string, run Runner) ([]string, error) {
timers, services, containers := unitsOf(known, module)
var stopped []string
for _, r := range append(timers, services...) {
manager := managerFor(r.Scope, r.User, run)
if state, err := sys.ServiceState(ctx, manager, r.Target); err != nil {
if strings.Contains(err.Error(), "does not exist on this machine") {
continue
}
return stopped, fmt.Errorf("stopping %s of %s: %w", r.Target, module, err)
} else if state == "stopped" {
continue
}
if err := sys.SetServiceState(ctx, manager, r.Target, "stopped"); err != nil {
return stopped, fmt.Errorf("stopping %s of %s: %w", r.Target, module, err)
}
stopped = append(stopped, r.Target)
}
if len(containers) == 0 {
return stopped, nil
}
cri, err := containerRuntime(ctx, run)
if err != nil {
return stopped, err
}
sort.Slice(containers, func(i, j int) bool { return containers[i].Target < containers[j].Target })
for _, r := range containers {
// A container found stopped is not in the answer, so it is not started again (H-A-752).
if state, err := containerState(ctx, r.Target, run); err != nil || !state.Running {
continue
}
if _, err := run(ctx, cri, "stop", r.Target); err != nil {
return stopped, fmt.Errorf("stopping the container %s of %s: %w", r.Target, module, err)
}
stopped = append(stopped, r.Target)
}
return stopped, nil
}