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,
|
||||
DashboardService: dashboardService,
|
||||
OPDSHandler: opdsHandler,
|
||||
Worker: worker,
|
||||
SystemSettingsHandler: systemSettingsHandler,
|
||||
ConnManager: connManager,
|
||||
QueueProcessor: queueProcessor,
|
||||
|
||||
+22
-20
@@ -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(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user