Reduce hot run polling pressure

This commit is contained in:
npc0-hue
2026-09-07 20:26:20 +08:00
parent 4ab7bb820d
commit fd9dbab669
16 changed files with 335 additions and 56 deletions
+51
View File
@@ -1,6 +1,7 @@
package repo
import (
"bytes"
"encoding/json"
"errors"
"os"
@@ -306,6 +307,56 @@ func TestMySQLSnapshotRoundTripsDurableJobSchedulingMetadata(t *testing.T) {
}
}
func TestFileStorePersistsRunRuntimeStateSeparately(t *testing.T) {
path := filepath.Join(t.TempDir(), "metadata.json")
store, err := NewFileStore(path)
if err != nil {
t.Fatalf("create file store: %v", err)
}
stamp := time.Date(2026, 7, 18, 10, 0, 0, 0, time.UTC)
if err := store.PluginDataRecords().Create(domain.PluginDataRecord{ID: "plugin-record-1", PluginID: "server.scum", ServerInstanceID: "server-1", Collection: "players", Key: "steam-1", Value: map[string]any{"displayName": "Ada"}, CreatedAt: stamp, UpdatedAt: stamp}); err != nil {
t.Fatalf("create plugin data: %v", err)
}
mainBefore, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read main snapshot before runtime update: %v", err)
}
endpoint := domain.RunEndpoint{ID: "run-runtime", DisplayName: "Runtime Run", Version: "1.0.0", Platform: "linux", Architecture: "amd64", Status: domain.RunEndpointStatusOnline, Capacity: domain.RunCapacity{MaxJobs: 4}, LastHeartbeatAt: stamp}
if err := store.RunEndpoints().Create(endpoint); err != nil {
t.Fatalf("create runtime endpoint: %v", err)
}
session := domain.RunControlSession{RunEndpointID: endpoint.ID, SessionToken: "raw-runtime-token", SessionTokenHash: strings.Repeat("a", 64), Status: domain.AuthSessionStatusActive, Generation: 1, CapabilityFingerprint: "cap-runtime", HeartbeatIntervalSeconds: 15, CreatedAt: stamp, UpdatedAt: stamp, ExpiresAt: stamp.Add(time.Hour), RequireSignedRequests: true, UsedNonces: []string{"nonce-1"}}
if err := store.RunControlSessions().Create(session); err != nil {
t.Fatalf("create runtime session: %v", err)
}
mainAfter, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read main snapshot after runtime update: %v", err)
}
if !bytes.Equal(mainBefore, mainAfter) {
t.Fatalf("runtime updates rewrote the main metadata snapshot")
}
runtimePayload, err := os.ReadFile(runtimeMetadataPath(path))
if err != nil {
t.Fatalf("read runtime snapshot: %v", err)
}
if !strings.Contains(string(runtimePayload), endpoint.ID) || strings.Contains(string(runtimePayload), session.SessionToken) {
t.Fatalf("unexpected runtime snapshot payload: %s", runtimePayload)
}
reloaded, err := NewFileStore(path)
if err != nil {
t.Fatalf("reload file store: %v", err)
}
if got, err := reloaded.RunEndpoints().Get(endpoint.ID); err != nil || got.Version != endpoint.Version {
t.Fatalf("runtime endpoint did not reload: %+v err=%v", got, err)
}
if got, err := reloaded.RunControlSessions().Get(endpoint.ID); err != nil || got.SessionTokenHash != session.SessionTokenHash || got.UsedNonces[0] != "nonce-1" {
t.Fatalf("runtime session did not reload: %+v err=%v", got, err)
}
}
func TestFileStorePersistsPluginOperationsStateAcrossRestart(t *testing.T) {
path := filepath.Join(t.TempDir(), "plugin-operations.json")
store, err := NewFileStore(path)