feat: implement health checks in daemon event loop (bd-gqo)
Add health checks to checkDaemonHealth() function: - Database integrity check using PRAGMA quick_check(1) - Disk space check with 100MB warning threshold (platform-specific) - Memory usage check with 500MB heap warning threshold Platform-specific disk space implementations: - Unix: uses unix.Statfs - Windows: uses windows.GetDiskFreeSpaceEx - WASM: returns unsupported (false) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/rpc"
|
||||
@@ -168,8 +169,9 @@ func runEventDrivenLoop(
|
||||
// Separate from sync operations - just validates state.
|
||||
//
|
||||
// Implements bd-e0o: Phase 3 daemon robustness for GH #353
|
||||
// Implements bd-gqo: Additional health checks
|
||||
func checkDaemonHealth(ctx context.Context, store storage.Storage, log daemonLogger) {
|
||||
// Health check: Verify metadata is accessible
|
||||
// Health check 1: Verify metadata is accessible
|
||||
// This helps detect if external operations (like bd import --force) have modified metadata
|
||||
// Without this, daemon may continue operating with stale metadata cache
|
||||
if _, err := store.GetMetadata(ctx, "last_import_hash"); err != nil {
|
||||
@@ -178,8 +180,38 @@ func checkDaemonHealth(ctx context.Context, store storage.Storage, log daemonLog
|
||||
// This helps diagnose stuck states in sandboxed environments
|
||||
}
|
||||
|
||||
// TODO(bd-gqo): Add additional health checks:
|
||||
// - Database integrity check
|
||||
// - Disk space check
|
||||
// - Memory usage check
|
||||
// Health check 2: Database integrity check
|
||||
// Verify the database is accessible and structurally sound
|
||||
if db := store.UnderlyingDB(); db != nil {
|
||||
// Quick integrity check - just verify we can query
|
||||
var result string
|
||||
if err := db.QueryRowContext(ctx, "PRAGMA quick_check(1)").Scan(&result); err != nil {
|
||||
log.log("Health check: database integrity check failed: %v", err)
|
||||
} else if result != "ok" {
|
||||
log.log("Health check: database integrity issue: %s", result)
|
||||
}
|
||||
}
|
||||
|
||||
// Health check 3: Disk space check (platform-specific)
|
||||
// Uses checkDiskSpace helper which is implemented per-platform
|
||||
dbPath := store.Path()
|
||||
if dbPath != "" {
|
||||
if availableMB, ok := checkDiskSpace(dbPath); ok {
|
||||
// Warn if less than 100MB available
|
||||
if availableMB < 100 {
|
||||
log.log("Health check: low disk space warning: %dMB available", availableMB)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Health check 4: Memory usage check
|
||||
// Log warning if memory usage is unusually high
|
||||
var memStats runtime.MemStats
|
||||
runtime.ReadMemStats(&memStats)
|
||||
heapMB := memStats.HeapAlloc / (1024 * 1024)
|
||||
|
||||
// Warn if heap exceeds 500MB (daemon should be lightweight)
|
||||
if heapMB > 500 {
|
||||
log.log("Health check: high memory usage warning: %dMB heap allocated", heapMB)
|
||||
}
|
||||
}
|
||||
|
||||
22
cmd/bd/daemon_health_unix.go
Normal file
22
cmd/bd/daemon_health_unix.go
Normal file
@@ -0,0 +1,22 @@
|
||||
//go:build !windows && !wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// checkDiskSpace returns the available disk space in MB for the given path.
|
||||
// Returns (availableMB, true) on success, (0, false) on failure.
|
||||
func checkDiskSpace(path string) (uint64, bool) {
|
||||
var stat unix.Statfs_t
|
||||
if err := unix.Statfs(path, &stat); err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Calculate available space in bytes, then convert to MB
|
||||
availableBytes := stat.Bavail * uint64(stat.Bsize)
|
||||
availableMB := availableBytes / (1024 * 1024)
|
||||
|
||||
return availableMB, true
|
||||
}
|
||||
10
cmd/bd/daemon_health_wasm.go
Normal file
10
cmd/bd/daemon_health_wasm.go
Normal file
@@ -0,0 +1,10 @@
|
||||
//go:build wasm
|
||||
|
||||
package main
|
||||
|
||||
// checkDiskSpace returns the available disk space in MB for the given path.
|
||||
// Returns (availableMB, true) on success, (0, false) on failure.
|
||||
// WASM builds don't support disk space checks.
|
||||
func checkDiskSpace(path string) (uint64, bool) {
|
||||
return 0, false
|
||||
}
|
||||
33
cmd/bd/daemon_health_windows.go
Normal file
33
cmd/bd/daemon_health_windows.go
Normal file
@@ -0,0 +1,33 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// checkDiskSpace returns the available disk space in MB for the given path.
|
||||
// Returns (availableMB, true) on success, (0, false) on failure.
|
||||
func checkDiskSpace(path string) (uint64, bool) {
|
||||
var freeBytesAvailable, totalBytes, totalFreeBytes uint64
|
||||
|
||||
pathPtr, err := windows.UTF16PtrFromString(path)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
err = windows.GetDiskFreeSpaceEx(
|
||||
pathPtr,
|
||||
(*uint64)(unsafe.Pointer(&freeBytesAvailable)),
|
||||
(*uint64)(unsafe.Pointer(&totalBytes)),
|
||||
(*uint64)(unsafe.Pointer(&totalFreeBytes)),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Convert to MB
|
||||
availableMB := freeBytesAvailable / (1024 * 1024)
|
||||
return availableMB, true
|
||||
}
|
||||
Reference in New Issue
Block a user