asnannp commited on
Commit
fc7eb7a
·
1 Parent(s): 995252b

fix(health): optimize backend health checks for Hugging Face multi-tenant environment

Browse files
Files changed (1) hide show
  1. backend/app/health_checks.py +17 -10
backend/app/health_checks.py CHANGED
@@ -433,10 +433,10 @@ def check_disk_space() -> CheckResult:
433
  usage = psutil.disk_usage(str(BACKEND_ROOT))
434
  usage_percent = usage.percent
435
 
436
- if usage_percent < 80:
437
  status = "ok"
438
  message = "Disk space healthy"
439
- elif usage_percent < 90:
440
  status = "degraded"
441
  message = f"Disk usage at {usage_percent:.1f}% — approaching capacity"
442
  else:
@@ -476,15 +476,18 @@ def check_memory_usage() -> CheckResult:
476
  system_percent = system_mem.percent
477
 
478
  # Determine status based on process memory
479
- if process_percent < 50 and system_percent < 80:
 
 
 
480
  status = "ok"
481
  message = "Memory usage healthy"
482
- elif process_percent < 80 and system_percent < 90:
483
  status = "degraded"
484
- message = f"Process memory at {process_percent:.1f}%, system at {system_percent:.1f}%"
485
  else:
486
  status = "error"
487
- message = f"Memory usage critical — process: {process_percent:.1f}%, system: {system_percent:.1f}%"
488
 
489
  return CheckResult(
490
  status=status,
@@ -516,15 +519,19 @@ def check_cpu_usage() -> CheckResult:
516
  process = psutil.Process(os.getpid())
517
  process_cpu = process.cpu_percent(interval=0.1)
518
 
519
- if cpu_percent < 80:
 
 
 
 
520
  status = "ok"
521
  message = "CPU usage healthy"
522
- elif cpu_percent < 90:
523
  status = "degraded"
524
- message = f"CPU usage elevated at {cpu_percent:.1f}%"
525
  else:
526
  status = "error"
527
- message = f"CPU usage critical at {cpu_percent:.1f}%"
528
 
529
  return CheckResult(
530
  status=status,
 
433
  usage = psutil.disk_usage(str(BACKEND_ROOT))
434
  usage_percent = usage.percent
435
 
436
+ if usage_percent < 85:
437
  status = "ok"
438
  message = "Disk space healthy"
439
+ elif usage_percent < 95:
440
  status = "degraded"
441
  message = f"Disk usage at {usage_percent:.1f}% — approaching capacity"
442
  else:
 
476
  system_percent = system_mem.percent
477
 
478
  # Determine status based on process memory
479
+ # In multi-tenant container environments (e.g., Hugging Face Spaces), host-wide system memory is shared
480
+ # and almost always >95%. We ignore system_percent to prevent false-alarm critical statuses,
481
+ # focusing purely on process memory health.
482
+ if process_percent < 50:
483
  status = "ok"
484
  message = "Memory usage healthy"
485
+ elif process_percent < 80:
486
  status = "degraded"
487
+ message = f"Process memory elevated at {process_percent:.1f}%"
488
  else:
489
  status = "error"
490
+ message = f"Memory usage critical — process: {process_percent:.1f}%"
491
 
492
  return CheckResult(
493
  status=status,
 
519
  process = psutil.Process(os.getpid())
520
  process_cpu = process.cpu_percent(interval=0.1)
521
 
522
+ # Determine health status based on our process load normalized per core.
523
+ # In multi-tenant environments (e.g., Hugging Face Spaces), system-wide CPU can be elevated by other spaces.
524
+ # If our own process is behaving well, we should remain 'ok'.
525
+ process_cpu_per_core = process_cpu / cpu_count if cpu_count else process_cpu
526
+ if process_cpu_per_core < 80:
527
  status = "ok"
528
  message = "CPU usage healthy"
529
+ elif process_cpu_per_core < 95:
530
  status = "degraded"
531
+ message = f"Process CPU usage elevated at {process_cpu_per_core:.1f}% per core"
532
  else:
533
  status = "error"
534
+ message = f"Process CPU usage critical at {process_cpu_per_core:.1f}% per core"
535
 
536
  return CheckResult(
537
  status=status,