From d740442ca4503f6f55013995e5256e125793ac98 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Thu, 5 Mar 2026 20:26:42 -0500 Subject: [PATCH] feat: refactor health check endpoint with real-time worker status Extract health check logic into GetHealth method on Config struct and integrate with Worker service for accurate scan status reporting. Changes: - Move health check handler from inline function to Config.GetHealth() - Add Worker field to Config struct for dependency injection - Wire Worker into main server dependencies - Report actual scan_in_progress status using Worker.HasActiveScans() - Report actual active_jobs count using Worker.GetActiveJobCount() This provides more accurate health monitoring by checking the real state of background jobs rather than returning static placeholder values. --- cmd/server/main.go | 1 + internal/router/frontend.go | 42 +++++++++++++++++++------------------ internal/router/router.go | 1 + 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index c206f9a..16516fb 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -175,6 +175,7 @@ func main() { DashboardHandler: dashboardHandler, DashboardService: dashboardService, OPDSHandler: opdsHandler, + Worker: worker, SystemSettingsHandler: systemSettingsHandler, ConnManager: connManager, QueueProcessor: queueProcessor, diff --git a/internal/router/frontend.go b/internal/router/frontend.go index 3b5c340..a4a88be 100644 --- a/internal/router/frontend.go +++ b/internal/router/frontend.go @@ -825,28 +825,30 @@ func registerFrontendRoutes(cfg *Config) { return c.HTML(http.StatusOK, buf.String()) }) - // ============================================================================ - // HEALTH CHECK - // ============================================================================ + e.GET("/health", cfg.GetHealth) +} - // Health check - e.GET("/health", func(c echo.Context) error { - ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second) - defer cancel() +// ============================================================================ +// HEALTH CHECK +// ============================================================================ - if err := pingDB(cfg, ctx); err != nil { - return c.JSON(http.StatusServiceUnavailable, map[string]string{ - "status": "unhealthy", - "error": err.Error(), - }) - } - return c.JSON(http.StatusOK, map[string]interface{}{ - "status": "healthy", - "database": "connected", - "scan": map[string]interface{}{ - "scan_in_progress": false, // Would check worker results - "active_jobs": 0, // Would count running jobs - }, +// Health check +func (cfg *Config) GetHealth(c echo.Context) error { + ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second) + defer cancel() + if err := pingDB(cfg, ctx); err != nil { + return c.JSON(http.StatusServiceUnavailable, map[string]string{ + "status": "unhealthy", + "error": err.Error(), }) + } + + return c.JSON(http.StatusOK, map[string]interface{}{ + "status": "healthy", + "database": "connected", + "scan": map[string]interface{}{ + "scan_in_progress": cfg.Worker.HasActiveScans(), + "active_jobs": cfg.Worker.GetActiveJobCount(), + }, }) } diff --git a/internal/router/router.go b/internal/router/router.go index beec0c2..eab6ded 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -52,6 +52,7 @@ type Config struct { AnalyticsHandler *handlers.AnalyticsHandler QueueHandler *handlers.QueueHandler CollectionHandler *handlers.CollectionHandler + Worker *services.Worker DashboardHandler *handlers.DashboardHandler DashboardService *services.DashboardService OPDSHandler *handlers.OPDSHandler