Skip to content

Commit

Permalink
fix failing build 503 caused by health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Developerayo committed Feb 10, 2025
1 parent 76bd46b commit 960a806
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
14 changes: 9 additions & 5 deletions api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ import (
// GetHealth retrieves the current health status of the system
func GetHealth(monitor *HealthMonitor) gin.HandlerFunc {
return func(c *gin.Context) {
status := monitor.GetHealthStatus()
status, err := monitor.GetHealthStatus()

httpStatus := http.StatusOK
if status.State == models.HealthStateRed {
httpStatus = http.StatusServiceUnavailable
response := gin.H{
"api_healthy": err == nil,
"status": nil,
}

c.JSON(httpStatus, status)
if err == nil {
response["status"] = status
}

c.JSON(http.StatusOK, response)
}
}

Expand Down
10 changes: 7 additions & 3 deletions api/health_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,17 @@ func (h *HealthMonitor) UpdateHealthState(newState models.HealthState, descripti
}

// GetHealthStatus returns the complete health status of the VM
func (h *HealthMonitor) GetHealthStatus() models.HealthStatus {
func (h *HealthMonitor) GetHealthStatus() (models.HealthStatus, error) {
h.mu.Lock()
defer h.mu.Unlock()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := h.db.PingContext(ctx); err != nil {
return models.HealthStatus{}, fmt.Errorf("database connection failed: %w", err)
}

if err := h.db.QueryRowContext(ctx, `SELECT 1 FROM daily_health_summaries WHERE date = $1`,
time.Now().UTC().Truncate(24*time.Hour)).Err(); err == sql.ErrNoRows {
models.UpdateDailyHealthSummary(h.db, h.currentStatus)
Expand Down Expand Up @@ -237,8 +241,8 @@ func (h *HealthMonitor) GetHealthStatus() models.HealthStatus {
}

if err := models.UpdateDailyHealthSummary(h.db, h.currentStatus); err != nil {
log.Printf("Error updating daily health summary: %v", err)
return h.currentStatus, fmt.Errorf("Error updating daily health summary: %w", err)
}

return h.currentStatus
return h.currentStatus, nil
}
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ func main() {
var lastState models.HealthState
var lastDate time.Time

status := healthMonitor.GetHealthStatus()
lastState = status.State
lastDate = time.Now().UTC().Truncate(24 * time.Hour)
status, err := healthMonitor.GetHealthStatus()
if err == nil {
lastState = status.State
lastDate = time.Now().UTC().Truncate(24 * time.Hour)
}

for range ticker.C {
status := healthMonitor.GetHealthStatus()
status, err := healthMonitor.GetHealthStatus()
if err != nil {
continue
}

currentDate := time.Now().UTC().Truncate(24 * time.Hour)

if status.State != lastState {
Expand Down

0 comments on commit 960a806

Please sign in to comment.