Spaces:
Sleeping
Sleeping
File size: 3,667 Bytes
67f25fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#!/usr/bin/env python3
"""
Universal Health Check Script
Monitors the health of the deployed application across different platforms
"""
import requests
import time
import sys
import os
from urllib.parse import urlparse
def check_health(url, timeout=30, retries=3):
"""Check if the service is healthy"""
print(f"π Checking health at: {url}")
for attempt in range(retries):
try:
response = requests.get(url, timeout=timeout)
if response.status_code == 200:
print(f"β
Service is healthy (attempt {attempt + 1})")
return True
else:
print(f"β οΈ Service returned status {response.status_code} (attempt {attempt + 1})")
except requests.exceptions.RequestException as e:
print(f"β Health check failed: {e} (attempt {attempt + 1})")
if attempt < retries - 1:
print(f"β³ Retrying in 5 seconds...")
time.sleep(5)
return False
def detect_platform():
"""Detect the current deployment platform"""
if os.getenv('RAILWAY_ENVIRONMENT'):
return 'railway'
elif os.getenv('RENDER_EXTERNAL_URL'):
return 'render'
elif os.getenv('HEROKU_APP_NAME'):
return 'heroku'
elif os.getenv('HF_SPACES'):
return 'huggingface'
elif os.path.exists('/.dockerenv'):
return 'docker'
else:
return 'local'
def get_health_urls():
"""Get health check URLs based on platform"""
platform = detect_platform()
print(f"π Detected platform: {platform}")
urls = []
if platform == 'railway':
# Railway provides environment variable for external URL
external_url = os.getenv('RAILWAY_STATIC_URL') or os.getenv('RAILWAY_PUBLIC_DOMAIN')
if external_url:
urls.append(f"https://{external_url}")
urls.append("http://localhost:8501")
elif platform == 'render':
external_url = os.getenv('RENDER_EXTERNAL_URL')
if external_url:
urls.append(external_url)
urls.append("http://localhost:8501")
elif platform == 'heroku':
app_name = os.getenv('HEROKU_APP_NAME')
if app_name:
urls.append(f"https://{app_name}.herokuapp.com")
urls.append("http://localhost:8501")
elif platform == 'huggingface':
# HF Spaces URL pattern
space_id = os.getenv('SPACE_ID')
if space_id:
urls.append(f"https://{space_id}.hf.space")
urls.append("http://localhost:7860") # HF Spaces default port
elif platform == 'docker':
urls.append("http://localhost:8501")
urls.append("http://localhost:8001/health") # Backend health
else: # local
urls.append("http://localhost:8501")
urls.append("http://localhost:8001/health") # Backend if running
return urls
def main():
"""Main health check function"""
print("=" * 50)
print("π₯ Multi-Lingual Catalog Translator Health Check")
print("=" * 50)
urls = get_health_urls()
if not urls:
print("β No health check URLs found")
sys.exit(1)
all_healthy = True
for url in urls:
if not check_health(url):
all_healthy = False
print(f"β Failed: {url}")
else:
print(f"β
Healthy: {url}")
print("-" * 30)
if all_healthy:
print("π All services are healthy!")
sys.exit(0)
else:
print("π₯ Some services are unhealthy!")
sys.exit(1)
if __name__ == "__main__":
main()
|