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:
@@ -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,
|
||||||
|
|||||||
+22
-20
@@ -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
|
||||||
ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second)
|
// ============================================================================
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err := pingDB(cfg, ctx); err != nil {
|
// Health check
|
||||||
return c.JSON(http.StatusServiceUnavailable, map[string]string{
|
func (cfg *Config) GetHealth(c echo.Context) error {
|
||||||
"status": "unhealthy",
|
ctx, cancel := context.WithTimeout(c.Request().Context(), 2*time.Second)
|
||||||
"error": err.Error(),
|
defer cancel()
|
||||||
})
|
if err := pingDB(cfg, ctx); err != nil {
|
||||||
}
|
return c.JSON(http.StatusServiceUnavailable, map[string]string{
|
||||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
"status": "unhealthy",
|
||||||
"status": "healthy",
|
"error": err.Error(),
|
||||||
"database": "connected",
|
|
||||||
"scan": map[string]interface{}{
|
|
||||||
"scan_in_progress": false, // Would check worker results
|
|
||||||
"active_jobs": 0, // Would count running jobs
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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(),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user