Files
archie/modules/connectors/health.go
T
sam f24c2b4455 feat(connectors): separate packages from scoped installations (#2027)
Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted.

The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
2026-11-24 03:05:27 +11:00

80 lines
2.2 KiB
Go

package connectors
import (
"cmp"
"maps"
"slices"
"time"
)
// Health is what the module knows about one installation right now. It is
// read from memory, so a database outage does not hide it.
type Health struct {
ID string `json:"id"`
Name string `json:"name"`
Scope string `json:"scope" enum:"instance,org,person"`
Contract string `json:"contract" doc:"connector, secretengine, guard or mcp"`
Healthy bool `json:"healthy"`
// Error is why the installation is not running.
Error string `json:"error,omitzero"`
// FailedAt is when the installation last failed to start. A process that
// exited after it started has no time.
FailedAt *time.Time `json:"failed_at,omitzero"`
}
// Health lists every installation 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)
launched := maps.Clone(m.launched)
servers := maps.Clone(m.servers)
started := map[string]bool{}
for id := range m.running {
started[id] = true
}
for id := range m.engines {
started[id] = true
}
for id := range m.guards {
started[id] = true
}
for id := range servers {
started[id] = true
}
m.mu.RUnlock()
ids := make([]string, 0, len(launched))
for id := range launched {
if started[id] || failed[id] != "" {
ids = append(ids, id)
}
}
slices.SortFunc(ids, func(a, b string) int {
return cmp.Or(cmp.Compare(launched[a].name, launched[b].name), cmp.Compare(a, b))
})
out := make([]Health, 0, len(ids))
for _, id := range ids {
info := launched[id]
h := Health{ID: id, Name: info.name, Scope: info.scope, Contract: info.contract, Healthy: true}
server, isServer := servers[id]
switch {
case failed[id] != "":
h.Healthy, h.Error = false, failed[id]
if at, ok := failedAt[id]; ok {
h.FailedAt = &at
}
case isServer:
if err := server.Ready(); err != nil {
h.Healthy, h.Error = false, err.Error()
}
case !m.host.Alive(id):
h.Healthy, h.Error = false, "process exited"
}
out = append(out, h)
}
return out
}