The settings page now reads apply status beside each resource: every known process with the version it applied, its error if it rejected the edit, and whether it is still reporting at all. It re-reads after each save, so the page reflects what the processes did with an edit rather than what was stored. The server derives liveness only, because it knows whether a record went stale but not which version the page is showing. The client compares against the resource it is rendering, which is what turns a live record on an older version into a pending restart. A process that stopped re-stamping and one that never reported are separate rows with separate wording, and neither is an error: the setting may be fine and the process simply absent, which an operator acts on differently. A failed apply-status read leaves the previous rows rather than taking the settings page down with it. state-store-contract.md gains the pair in its inventory and its count moves from 45 to 47, with the note that this read is a list rather than the config-snapshot pair's found=false singleton. Closes archie-core-pskb.
81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package webui
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// The readings an operator acts on. A process is current when it is running
|
|
// what it last reported, failed when it rejected what the store holds, and
|
|
// unknown when it has stopped re-stamping: a record that outlived its process
|
|
// must never read as current (docs/prds/control-plane-apply-status.md).
|
|
const (
|
|
applyStateCurrent = "current"
|
|
applyStateFailed = "failed"
|
|
applyStateUnknown = "unknown"
|
|
)
|
|
|
|
type applyStatusRecord struct {
|
|
Process string `json:"process"`
|
|
Kind string `json:"kind"`
|
|
AppliedVersion int64 `json:"applied_version"`
|
|
Error string `json:"error,omitempty"`
|
|
ReportedAt time.Time `json:"reported_at"`
|
|
State string `json:"state"`
|
|
}
|
|
|
|
type applyStatusView struct {
|
|
Records []applyStatusRecord `json:"records"`
|
|
// Processes is every name a record may carry, so the page can show a
|
|
// process that has never reported at all rather than omitting the column.
|
|
Processes []string `json:"processes"`
|
|
}
|
|
|
|
// handleApplyStatus reports which version of each control-plane resource each
|
|
// process is running. A process with no apply-status wiring renders an empty
|
|
// page rather than failing: that is a deployment fact, not an error.
|
|
func (s *Server) handleApplyStatus(w http.ResponseWriter, r *http.Request) {
|
|
view := applyStatusView{Records: []applyStatusRecord{}, Processes: applystatus.Processes()}
|
|
if s.ApplyStatus == nil {
|
|
writeJSON(w, view)
|
|
return
|
|
}
|
|
records, err := s.ApplyStatus.ListApplyStatus(r.Context())
|
|
if err != nil {
|
|
http.Error(w, "apply status unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
now := s.clock()
|
|
for _, record := range records {
|
|
view.Records = append(view.Records, applyStatusRecord{
|
|
Process: record.Process, Kind: record.Kind, AppliedVersion: record.AppliedVersion,
|
|
Error: record.Error, ReportedAt: record.ReportedAt, State: applyState(record, now),
|
|
})
|
|
}
|
|
writeJSON(w, view)
|
|
}
|
|
|
|
// applyState reads staleness before the error: a process that stopped
|
|
// reporting is unknown whatever its last report said, because that report
|
|
// describes a process that may no longer exist.
|
|
func applyState(record storecontract.ApplyStatus, now time.Time) string {
|
|
switch {
|
|
case applystatus.Stale(record.ReportedAt, now):
|
|
return applyStateUnknown
|
|
case record.Error != "":
|
|
return applyStateFailed
|
|
default:
|
|
return applyStateCurrent
|
|
}
|
|
}
|
|
|
|
func (s *Server) clock() time.Time {
|
|
if s.now != nil {
|
|
return s.now()
|
|
}
|
|
return time.Now().UTC()
|
|
}
|