// Package readiness implements the health probes. package readiness import ( "context" "errors" "fmt" "os" "strings" "syscall" "time" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/infrastructure/access" ) // --- state_db --- // statusCounter is the read-only store surface the state_db probe needs. // The task store satisfies it; the probe never depends on the // full task-lifecycle interface. type statusCounter interface { StatusCounts(ctx context.Context) (map[string]int, error) } // StoreProbe checks the state database with a cheap read-only query // (StatusCounts). It is the store's own health signal: a lock, corruption, // or connection failure surfaces as an error here. type StoreProbe struct { Store statusCounter } // NewStoreProbe returns a state_db probe over the given store. func NewStoreProbe(s statusCounter) *StoreProbe { return &StoreProbe{Store: s} } func (p *StoreProbe) Name() string { return "state_db" } func (p *StoreProbe) Check(ctx context.Context) health.Result { if p.Store == nil { return health.Result{Status: health.StatusDegraded, Detail: "store not wired"} } if _, err := p.Store.StatusCounts(ctx); err != nil { return health.Result{Status: health.StatusDegraded, Detail: "read failed: " + err.Error()} } return health.Result{Status: health.StatusOK} } // --- remote contract --- // ContractProbe checks that a remote service answers a cheap call within a // timeout. type ContractProbe struct { ProbeName string Timeout time.Duration Ping func(context.Context) error } // NewContractProbe returns a probe named name that calls ping. A zero or // negative timeout leaves the caller's context deadline in force. func NewContractProbe(name string, timeout time.Duration, ping func(context.Context) error) *ContractProbe { return &ContractProbe{ProbeName: name, Timeout: timeout, Ping: ping} } func (p *ContractProbe) Name() string { return p.ProbeName } func (p *ContractProbe) Check(ctx context.Context) health.Result { if p.Ping == nil { return health.Result{Status: health.StatusDegraded, Detail: "contract not wired"} } if p.Timeout > 0 { var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, p.Timeout) defer cancel() } if err := p.Ping(ctx); err != nil { return health.Result{Status: health.StatusDegraded, Detail: "unreachable: " + err.Error()} } return health.Result{Status: health.StatusOK} } // --- access policies --- // PolicyProblems reports the stored access policies this process's engine // could not validate. The policy chain is the source; a policy that failed // startup re-validation denies its level and must be named here with its // error. Ready is false until the stored policies have loaded. type PolicyProblems interface { Ready() bool Problems() []access.Problem } // ProblemProbe reports the stored policies the engine could not validate. type ProblemProbe struct { ProbeName string Source PolicyProblems } // NewProblemProbe returns an access_policies probe over a problem source. // A nil source degrades the probe rather than lying about the chain. func NewProblemProbe(name string, source PolicyProblems) *ProblemProbe { return &ProblemProbe{ProbeName: name, Source: source} } func (p *ProblemProbe) Name() string { return p.ProbeName } func (p *ProblemProbe) Check(ctx context.Context) health.Result { if p.Source == nil { return health.Result{Status: health.StatusDegraded, Detail: "access engine not wired"} } if !p.Source.Ready() { return health.Result{Status: health.StatusDegraded, Detail: "access policies have not loaded; requests are refused"} } problems := p.Source.Problems() if len(problems) == 0 { return health.Result{Status: health.StatusOK} } details := make([]string, 0, len(problems)) for _, problem := range problems { details = append(details, problem.Policy.ID+": "+problem.Err.Error()) } return health.Result{Status: health.StatusDegraded, Detail: strings.Join(details, "; ")} } // --- config --- // ConfigProbe validates the running configuration. It is the same check the // loader applies before accepting a config, run against the live snapshot. // The validation function is injected so this probe does not import the // configuration package directly; composition passes configuration.Validate. type ConfigProbe struct { Get func() config.Config Validate func(*config.Config) error } // NewConfigProbe returns a config probe over the running-config getter and // validator. Either may be nil; a nil validator degrades the probe. func NewConfigProbe(get func() config.Config, validate func(*config.Config) error) *ConfigProbe { return &ConfigProbe{Get: get, Validate: validate} } func (p *ConfigProbe) Name() string { return "config" } func (p *ConfigProbe) Check(ctx context.Context) health.Result { if p.Get == nil { return health.Result{Status: health.StatusDegraded, Detail: "config not wired"} } cfg := p.Get() if p.Validate == nil { return health.Result{Status: health.StatusDegraded, Detail: "config validator not wired"} } if err := p.Validate(&cfg); err != nil { return health.Result{Status: health.StatusDegraded, Detail: "invalid: " + err.Error()} } return health.Result{Status: health.StatusOK} } // --- disk --- // DefaultDiskThreshold is the fraction of a filesystem that, when in use, // makes the disk probe report degraded. It matches the "disk < 90% used" // requirement of the readiness epic. const DefaultDiskThreshold = 0.90 // DiskTarget is one filesystem location to measure. Optional targets are // ignored when they disappear (for example, /var on a non-Linux installation). type DiskTarget struct { Name string Path string Optional bool } // DiskProbe checks each configured filesystem independently and aggregates the // results. Statfs is injectable so callers can test disk boundaries without // filling a filesystem. type DiskProbe struct { // Path is retained for source compatibility with the original single-path // probe. Targets takes precedence when non-empty. Path string Targets []DiskTarget Threshold float64 Statfs func(string, *syscall.Statfs_t) error } // NewDiskProbeTargets returns a disk probe for the supplied, stable-ordered targets. func NewDiskProbeTargets(targets []DiskTarget) *DiskProbe { return &DiskProbe{Targets: append([]DiskTarget(nil), targets...), Threshold: DefaultDiskThreshold, Statfs: syscall.Statfs} } func (p *DiskProbe) Name() string { return "disk" } // reduceTargets keeps one target per path, in first-seen position, with the // strictest requirement for that path. func reduceTargets(targets []DiskTarget) []DiskTarget { index := make(map[string]int, len(targets)) out := make([]DiskTarget, 0, len(targets)) for _, target := range targets { if target.Path == "" { continue } if at, ok := index[target.Path]; ok { if !target.Optional && out[at].Optional { out[at] = target } continue } index[target.Path] = len(out) out = append(out, target) } return out } func (p *DiskProbe) Check(ctx context.Context) health.Result { _ = ctx targets := p.Targets if len(targets) == 0 && p.Path != "" { targets = []DiskTarget{{Name: "work", Path: p.Path}} } if len(targets) == 0 { return health.Result{Status: health.StatusDegraded, Detail: "no disk targets configured"} } statfs := p.Statfs if statfs == nil { statfs = syscall.Statfs } // A degraded result names only the failing filesystems: a healthy one // beside it is noise to the operator reading why. var details, problems []string for _, target := range reduceTargets(targets) { var stat syscall.Statfs_t if err := statfs(target.Path, &stat); err != nil { if target.Optional && errors.Is(err, os.ErrNotExist) { continue } problems = append(problems, fmt.Sprintf("%s %s: statfs failed: %v", target.Name, target.Path, err)) continue } if stat.Blocks == 0 { problems = append(problems, fmt.Sprintf("%s %s: cannot size filesystem", target.Name, target.Path)) continue } used := stat.Blocks - stat.Bavail pct := float64(used) / float64(stat.Blocks) if pct >= p.Threshold { problems = append(problems, fmt.Sprintf("%s %s is %.0f%% full (limit %.0f%%)", target.Name, target.Path, pct*100, p.Threshold*100)) continue } details = append(details, fmt.Sprintf("%s %.0f%%", target.Name, pct*100)) } if len(problems) > 0 { return health.Result{Status: health.StatusDegraded, Detail: strings.Join(problems, "; ")} } if len(details) == 0 { for _, target := range targets { if !target.Optional { return health.Result{Status: health.StatusDegraded, Detail: "no disk path configured"} } } return health.Result{Status: health.StatusOK, Detail: "no optional disk targets present"} } return health.Result{Status: health.StatusOK, Detail: strings.Join(details, ", ")} } // --- model --- // ChannelState is a per-channel lifecycle fact as the gateway probe sees it. // It is a projection of the channels.Manager's Status, kept in this package // so the probe does not depend on the channels package's concrete type. type ChannelState struct { ID string Configured bool State string } // sessionCounter returns the number of currently connected/known sessions. type sessionCounter func(context.Context) int // GatewayProbe reports the gateway's running/draining state and its // connected session count. It consumes injected functions so it stays // independent of the channels and gateway packages. type GatewayProbe struct { Channels func() []ChannelState CountSessions sessionCounter }