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.
This commit is contained in:
2026-03-05 20:26:42 -05:00
parent e8efc2ee3e
commit d740442ca4
3 changed files with 24 additions and 20 deletions
+1
View File
@@ -175,6 +175,7 @@ func main() {
DashboardHandler: dashboardHandler, DashboardHandler: dashboardHandler,
DashboardService: dashboardService, DashboardService: dashboardService,
OPDSHandler: opdsHandler, OPDSHandler: opdsHandler,
Worker: worker,
SystemSettingsHandler: systemSettingsHandler, SystemSettingsHandler: systemSettingsHandler,
ConnManager: connManager, ConnManager: connManager,
QueueProcessor: queueProcessor, QueueProcessor: queueProcessor,
+11 -9
View File
@@ -825,28 +825,30 @@ func registerFrontendRoutes(cfg *Config) {
return c.HTML(http.StatusOK, buf.String()) return c.HTML(http.StatusOK, buf.String())
}) })
// ============================================================================ e.GET("/health", cfg.GetHealth)
// HEALTH CHECK }
// ============================================================================
// Health check // ============================================================================
e.GET("/health", func(c echo.Context) error { // HEALTH CHECK
// ============================================================================
// Health check
func (cfg *Config) GetHealth(c echo.Context) error {
ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second) ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second)
defer cancel() defer cancel()
if err := pingDB(cfg, ctx); err != nil { if err := pingDB(cfg, ctx); err != nil {
return c.JSON(http.StatusServiceUnavailable, map[string]string{ return c.JSON(http.StatusServiceUnavailable, map[string]string{
"status": "unhealthy", "status": "unhealthy",
"error": err.Error(), "error": err.Error(),
}) })
} }
return c.JSON(http.StatusOK, map[string]interface{}{ return c.JSON(http.StatusOK, map[string]interface{}{
"status": "healthy", "status": "healthy",
"database": "connected", "database": "connected",
"scan": map[string]interface{}{ "scan": map[string]interface{}{
"scan_in_progress": false, // Would check worker results "scan_in_progress": cfg.Worker.HasActiveScans(),
"active_jobs": 0, // Would count running jobs "active_jobs": cfg.Worker.GetActiveJobCount(),
}, },
}) })
})
} }
+1
View File
@@ -52,6 +52,7 @@ type Config struct {
AnalyticsHandler *handlers.AnalyticsHandler AnalyticsHandler *handlers.AnalyticsHandler
QueueHandler *handlers.QueueHandler QueueHandler *handlers.QueueHandler
CollectionHandler *handlers.CollectionHandler CollectionHandler *handlers.CollectionHandler
Worker *services.Worker
DashboardHandler *handlers.DashboardHandler DashboardHandler *handlers.DashboardHandler
DashboardService *services.DashboardService DashboardService *services.DashboardService
OPDSHandler *handlers.OPDSHandler OPDSHandler *handlers.OPDSHandler