Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Auto-recover corrupted unified_resources.db instead of looping 500s

rcourtman1a1184d27f5f
diff --git a/internal/unifiedresources/store.go b/internal/unifiedresources/store.go
index cf444c594a..3e4cf0a5ea 100644
--- a/internal/unifiedresources/store.go
+++ b/internal/unifiedresources/store.go
@@ -164,18 +164,81 @@ func NewSQLiteResourceStore(dataDir, orgID string) (*SQLiteResourceStore, error)
store := &SQLiteResourceStore{db: db, dbPath: path}
if err := store.initSchema(); err != nil {
wrappedInitErr := fmt.Errorf("initialize resource store schema for %q: %w", path, err)
+ if !isCorruptionError(err) {
+ if closeErr := db.Close(); closeErr != nil {
+ return nil, errors.Join(
+ wrappedInitErr,
+ fmt.Errorf("close resources db %q after init failure: %w", path, closeErr),
+ )
+ }
+ return nil, wrappedInitErr
+ }
+
+ log.Printf("[WARN] unified_resources: database %q appears corrupted (%v); backing up and recreating", path, err)
if closeErr := db.Close(); closeErr != nil {
return nil, errors.Join(
wrappedInitErr,
- fmt.Errorf("close resources db %q after init failure: %w", path, closeErr),
+ fmt.Errorf("close corrupted resources db %q: %w", path, closeErr),
)
}
- return nil, wrappedInitErr
+
+ if backupErr := backupCorruptedDB(path); backupErr != nil {
+ return nil, errors.Join(wrappedInitErr, fmt.Errorf("backup corrupted db %q: %w", path, backupErr))
+ }
+
+ db, err = sql.Open("sqlite", dsn)
+ if err != nil {
+ return nil, fmt.Errorf("reopen resources db after corruption recovery: %w", err)
+ }
+ db.SetMaxOpenConns(1)
+ db.SetMaxIdleConns(1)
+ db.SetConnMaxLifetime(0)
+
+ store = &SQLiteResourceStore{db: db, dbPath: path}
+ if err := store.initSchema(); err != nil {
+ if closeErr := db.Close(); closeErr != nil {
+ return nil, errors.Join(
+ fmt.Errorf("initialize resource store schema for %q after recovery: %w", path, err),
+ fmt.Errorf("close resources db %q after post-recovery init failure: %w", path, closeErr),
+ )
+ }
+ return nil, fmt.Errorf("initialize resource store schema for %q after recovery: %w", path, err)
+ }
+ log.Printf("[INFO] unified_resources: database %q recreated successfully after corruption recovery", path)
}
store.retentionStop = store.startRetentionLoop()
return store, nil
}
+// isCorruptionError reports whether the init error indicates a corrupted
+// database that warrants a delete-and-recreate recovery.
+func isCorruptionError(err error) bool {
+ if err == nil {
+ return false
+ }
+ msg := strings.ToLower(err.Error())
+ return strings.Contains(msg, "malformed") ||
+ strings.Contains(msg, "database disk image") ||
+ strings.Contains(msg, "file is not a database") ||
+ strings.Contains(msg, "database is not a database")
+}
+
+// backupCorruptedDB renames the corrupted database and its sidecar files
+// (WAL, SHM) to *.corrupted so a fresh database can be created in their place.
+func backupCorruptedDB(path string) error {
+ suffix := fmt.Sprintf(".corrupted.%d", time.Now().Unix())
+ sidecars := []string{path, path + "-wal", path + "-shm"}
+ for _, f := range sidecars {
+ if _, err := os.Stat(f); err != nil {
+ continue
+ }
+ if err := os.Rename(f, f+suffix); err != nil {
+ return fmt.Errorf("rename %q: %w", f, err)
+ }
+ }
+ return nil
+}
+
func normalizeOrgID(orgID string) (string, error) {
orgID = strings.TrimSpace(orgID)
if orgID == "" {
diff --git a/internal/unifiedresources/store_test.go b/internal/unifiedresources/store_test.go
index ada7ce26e6..880d9aa4eb 100644
--- a/internal/unifiedresources/store_test.go
+++ b/internal/unifiedresources/store_test.go
@@ -95,6 +95,32 @@ func TestNewSQLiteResourceStore_RejectsInvalidOrgID(t *testing.T) {
}
}
+func TestNewSQLiteResourceStore_RecoversFromCorruptedDB(t *testing.T) {
+ dataDir := t.TempDir()
+
+ store, err := NewSQLiteResourceStore(dataDir, "default")
+ if err != nil {
+ t.Fatalf("initial store: %v", err)
+ }
+ store.Close()
+
+ dbPath := filepath.Join(dataDir, "resources", "unified_resources.db")
+ if err := os.WriteFile(dbPath, []byte("this is not a valid sqlite database"), 0o600); err != nil {
+ t.Fatalf("corrupt database: %v", err)
+ }
+
+ recovered, err := NewSQLiteResourceStore(dataDir, "default")
+ if err != nil {
+ t.Fatalf("expected recovery from corrupted db, got error: %v", err)
+ }
+ defer recovered.Close()
+
+ matches, err := filepath.Glob(dbPath + ".corrupted.*")
+ if err != nil || len(matches) == 0 {
+ t.Fatal("expected corrupted database to be backed up")
+ }
+}
+
func TestNewSQLiteResourceStore_MigratesLegacyStore(t *testing.T) {
dataDir := t.TempDir()
orgID := "org.a"