Files
archie/modules/connectors/health.go
T

74 lines
2.0 KiB
Go

package connectors
import (
"maps"
"slices"
"time"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
// Health is what the module knows about one accepted package right now. It is
// read from memory, so a database outage does not hide it.
type Health struct {
Name string `json:"name"`
Contract string `json:"contract" doc:"connector, secretengine, guard or mcp"`
Healthy bool `json:"healthy"`
// Error is why the package is not running.
Error string `json:"error,omitzero"`
// FailedAt is when the package last failed to start. A process that exited
// after it started has no time.
FailedAt *time.Time `json:"failed_at,omitzero"`
}
// Health lists every package the module launched, in name order: those running,
// those that failed to start, and those whose process has since exited or whose
// MCP server stopped answering.
func (m *Module) Health() []Health {
m.mu.RLock()
failed := maps.Clone(m.failed)
failedAt := maps.Clone(m.failedAt)
contracts := maps.Clone(m.contracts)
servers := maps.Clone(m.servers)
names := map[string]string{}
for name := range m.running {
names[name] = store.ContractConnector
}
for name := range m.engines {
names[name] = store.ContractSecretEngine
}
for name := range m.guards {
names[name] = store.ContractGuard
}
for name := range servers {
names[name] = store.ContractMCP
}
for name := range failed {
if _, ok := names[name]; !ok {
names[name] = contracts[name]
}
}
m.mu.RUnlock()
out := make([]Health, 0, len(names))
for _, name := range slices.Sorted(maps.Keys(names)) {
h := Health{Name: name, Contract: names[name], Healthy: true}
server, isServer := servers[name]
switch {
case failed[name] != "":
h.Healthy, h.Error = false, failed[name]
if at, ok := failedAt[name]; ok {
h.FailedAt = &at
}
case isServer:
if err := server.Ready(); err != nil {
h.Healthy, h.Error = false, err.Error()
}
case !m.host.Alive(name):
h.Healthy, h.Error = false, "process exited"
}
out = append(out, h)
}
return out
}