#!/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()