@@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"neuralhunt/internal/auth"
|
||||
"neuralhunt/internal/data"
|
||||
rtx "neuralhunt/internal/runtime"
|
||||
)
|
||||
|
||||
func TestProfileCleanupPreviewProtectsConnectedClient(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := data.OpenSQLite(ctx, t.TempDir()+"/cleanup.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
store := data.New(db)
|
||||
jwk := auth.PublicJWK{Kty: "EC", Crv: "P-256", X: "AQ", Y: "Ag"}
|
||||
for _, id := range []string{"offline_old", "online_old"} {
|
||||
if err := store.UpsertClient(ctx, id, jwk); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
old := time.Now().UTC().Add(-10 * 24 * time.Hour).UnixMilli()
|
||||
if _, err := db.ExecContext(ctx, `UPDATE clients SET last_seen=?`, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtimeState := rtx.New()
|
||||
if _, err := runtimeState.AcquirePresence("online_old", "session"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := &Server{store: store, runtime: runtimeState}
|
||||
preview, ids, err := s.profileCleanupPreview(ctx, 7*24*60*60)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if preview.Eligible != 1 || preview.ProtectedConnected != 1 {
|
||||
t.Fatalf("unexpected preview: %+v", preview)
|
||||
}
|
||||
if len(ids) != 1 || ids[0] != "offline_old" {
|
||||
t.Fatalf("unexpected ids: %#v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfileCleanupDurationGuard(t *testing.T) {
|
||||
if _, err := profileCleanupDurationSeconds("3599"); err == nil {
|
||||
t.Fatal("sub-hour cleanup window should be rejected")
|
||||
}
|
||||
if got, err := profileCleanupDurationSeconds("86400"); err != nil || got != 86400 {
|
||||
t.Fatalf("got=%d err=%v", got, err)
|
||||
}
|
||||
}
|
||||
+115
-1
@@ -178,6 +178,8 @@ func (s *Server) Routes() http.Handler {
|
||||
r.Use(func(n http.Handler) http.Handler { return s.require("admin", n) })
|
||||
r.Get("/api/admin/overview", s.adminOverview)
|
||||
r.Get("/api/admin/performance", s.adminPerformance)
|
||||
r.Get("/api/admin/profiles/cleanup-preview", s.adminProfileCleanupPreview)
|
||||
r.Post("/api/admin/profiles/cleanup", s.adminProfileCleanup)
|
||||
r.Get("/api/admin/settings", s.adminSettingsGet)
|
||||
r.Put("/api/admin/settings", s.adminSettingsPut)
|
||||
r.Get("/api/admin/tasks", s.adminTasks)
|
||||
@@ -736,6 +738,108 @@ func (s *Server) serveAdminArtifactPart(w http.ResponseWriter, r *http.Request,
|
||||
http.ServeFile(w, r, path)
|
||||
}
|
||||
|
||||
type profileCleanupPreview struct {
|
||||
CutoffMS int64 `json:"cutoff_ms"`
|
||||
InactiveForSeconds int64 `json:"inactive_for_seconds"`
|
||||
Eligible int `json:"eligible"`
|
||||
ProtectedWinners int64 `json:"protected_winners"`
|
||||
ProtectedConnected int `json:"protected_connected"`
|
||||
OldestEligibleMS int64 `json:"oldest_eligible_ms,omitempty"`
|
||||
NewestEligibleMS int64 `json:"newest_eligible_ms,omitempty"`
|
||||
}
|
||||
|
||||
func profileCleanupDurationSeconds(raw string) (int64, error) {
|
||||
seconds, err := strconv.ParseInt(strings.TrimSpace(raw), 10, 64)
|
||||
if err != nil {
|
||||
return 0, errors.New("inactive_for_seconds must be an integer")
|
||||
}
|
||||
// A one-hour minimum prevents an accidental near-live purge while still
|
||||
// allowing short-lived development/test deployments to clean up quickly.
|
||||
if seconds < 3600 || seconds > 10*365*24*60*60 {
|
||||
return 0, errors.New("inactive_for_seconds must be between 3600 seconds and 10 years")
|
||||
}
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
func (s *Server) profileCleanupPreview(ctx context.Context, inactiveForSeconds int64) (profileCleanupPreview, []string, error) {
|
||||
cutoff := time.Now().UTC().Add(-time.Duration(inactiveForSeconds) * time.Second).UnixMilli()
|
||||
candidates, err := s.store.InactiveNonWinnerClients(ctx, cutoff)
|
||||
if err != nil {
|
||||
return profileCleanupPreview{}, nil, err
|
||||
}
|
||||
protectedWinners, err := s.store.OldWinnerCount(ctx, cutoff)
|
||||
if err != nil {
|
||||
return profileCleanupPreview{}, nil, err
|
||||
}
|
||||
ids := make([]string, 0, len(candidates))
|
||||
out := profileCleanupPreview{CutoffMS: cutoff, InactiveForSeconds: inactiveForSeconds, ProtectedWinners: protectedWinners}
|
||||
for _, c := range candidates {
|
||||
if s.runtime.IsConnected(c.ClientID) {
|
||||
out.ProtectedConnected++
|
||||
continue
|
||||
}
|
||||
ids = append(ids, c.ClientID)
|
||||
if out.OldestEligibleMS == 0 || c.LastSeen < out.OldestEligibleMS {
|
||||
out.OldestEligibleMS = c.LastSeen
|
||||
}
|
||||
if c.LastSeen > out.NewestEligibleMS {
|
||||
out.NewestEligibleMS = c.LastSeen
|
||||
}
|
||||
}
|
||||
out.Eligible = len(ids)
|
||||
return out, ids, nil
|
||||
}
|
||||
|
||||
func (s *Server) adminProfileCleanupPreview(w http.ResponseWriter, r *http.Request) {
|
||||
seconds, err := profileCleanupDurationSeconds(r.URL.Query().Get("inactive_for_seconds"))
|
||||
if err != nil {
|
||||
jsonOut(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
preview, _, err := s.profileCleanupPreview(r.Context(), seconds)
|
||||
if err != nil {
|
||||
jsonOut(w, 500, map[string]string{"error": "profile cleanup preview failed: " + err.Error()})
|
||||
return
|
||||
}
|
||||
jsonOut(w, 200, preview)
|
||||
}
|
||||
|
||||
func (s *Server) adminProfileCleanup(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
InactiveForSeconds int64 `json:"inactive_for_seconds"`
|
||||
}
|
||||
if err := decode(r, &in); err != nil {
|
||||
jsonOut(w, 400, map[string]string{"error": "bad json: " + err.Error()})
|
||||
return
|
||||
}
|
||||
seconds, err := profileCleanupDurationSeconds(strconv.FormatInt(in.InactiveForSeconds, 10))
|
||||
if err != nil {
|
||||
jsonOut(w, 400, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
preview, ids, err := s.profileCleanupPreview(r.Context(), seconds)
|
||||
if err != nil {
|
||||
jsonOut(w, 500, map[string]string{"error": "profile cleanup check failed: " + err.Error()})
|
||||
return
|
||||
}
|
||||
deleted, err := s.store.DeleteInactiveNonWinnerClients(r.Context(), preview.CutoffMS, ids)
|
||||
if err != nil {
|
||||
jsonOut(w, 500, map[string]string{"error": "profile cleanup failed: " + err.Error()})
|
||||
return
|
||||
}
|
||||
for _, id := range deleted {
|
||||
s.runtime.ForgetClient(id)
|
||||
}
|
||||
jsonOut(w, 200, map[string]any{
|
||||
"deleted": len(deleted),
|
||||
"eligible_before": preview.Eligible,
|
||||
"protected_winners": preview.ProtectedWinners,
|
||||
"protected_connected": preview.ProtectedConnected,
|
||||
"cutoff_ms": preview.CutoffMS,
|
||||
"inactive_for_seconds": seconds,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) adminOverview(w http.ResponseWriter, r *http.Request) {
|
||||
var clients, activeTasks, completedTasks, guesses, artifacts int64
|
||||
_ = s.store.DB.QueryRowContext(r.Context(), `SELECT count(*) FROM clients`).Scan(&clients)
|
||||
@@ -1062,6 +1166,10 @@ func (s *Server) ws(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "identity not registered", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
// WebSocket use counts as recent profile activity. This also makes a race
|
||||
// with the admin cleanup safe: a newly connecting identity no longer
|
||||
// matches an old last_seen cutoff.
|
||||
s.store.TouchClient(r.Context(), c.ClientID)
|
||||
t, err := s.store.TaskForClient(r.Context(), c.ClientID)
|
||||
if err != nil {
|
||||
http.Error(w, "no task", 503)
|
||||
@@ -1080,7 +1188,13 @@ func (s *Server) ws(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
cl := wsx.NewClient(conn, t.ID, c.ClientID, false)
|
||||
s.hub.Add(cl)
|
||||
defer func() { s.hub.Remove(cl); s.runtime.ReleasePresence(c.ClientID, c.SessionID, leaseID) }()
|
||||
defer func() {
|
||||
s.hub.Remove(cl)
|
||||
s.runtime.ReleasePresence(c.ClientID, c.SessionID, leaseID)
|
||||
// Mark the disconnect time as last activity. A client that stayed online
|
||||
// for days therefore starts its inactivity window only after disconnect.
|
||||
s.store.TouchClient(context.Background(), c.ClientID)
|
||||
}()
|
||||
|
||||
// A map point is durable only once per client/task. Subsequent losing guesses
|
||||
// stay in memory; improvements are checkpointed by the guess handler.
|
||||
|
||||
Reference in New Issue
Block a user