cardserver / tests /monitor_space.py
GitHub Actions
πŸš€ Auto-deploy from GitHub
63b8c2c
#!/usr/bin/env python3
"""
Monitor HF Space deployment status.
Checks if the space is running and provides status updates.
"""
import requests
import time
import sys
from datetime import datetime
def check_space_status(base_url: str) -> dict:
"""Check if the HF Space is running and responding."""
try:
# Try the root endpoint first
response = requests.get(f"{base_url}/", timeout=10)
if response.status_code == 200:
return {"status": "running", "message": "Space is active and responding"}
elif response.status_code == 404:
# Check if it's the HF "space not found" 404 or our app's 404
if "huggingface" in response.text.lower():
return {"status": "not_found", "message": "Space not found or not public"}
else:
return {"status": "app_404", "message": "App is running but endpoint not found"}
else:
return {"status": "error", "message": f"Unexpected status code: {response.status_code}"}
except requests.exceptions.Timeout:
return {"status": "timeout", "message": "Space is not responding (timeout)"}
except requests.exceptions.ConnectionError:
return {"status": "connection_error", "message": "Cannot connect to space"}
except Exception as e:
return {"status": "error", "message": f"Error: {str(e)}"}
def monitor_space(base_url: str, check_interval: int = 30, max_checks: int = 20):
"""Monitor the space status over time."""
print(f"πŸ” Monitoring HF Space: {base_url}")
print(f"⏱️ Check interval: {check_interval} seconds")
print(f"πŸ”’ Max checks: {max_checks}")
print("-" * 60)
for i in range(max_checks):
timestamp = datetime.now().strftime("%H:%M:%S")
print(f"[{timestamp}] Check {i+1}/{max_checks}:", end=" ")
status = check_space_status(base_url)
if status["status"] == "running":
print(f"βœ… {status['message']}")
print(f"πŸŽ‰ Space is running! You can now test the endpoints.")
return True
elif status["status"] == "app_404":
print(f"⚠️ {status['message']}")
print(f"πŸ’‘ App seems to be running, try the API endpoints directly.")
return True
else:
print(f"❌ {status['message']}")
if i < max_checks - 1:
print(f" Waiting {check_interval} seconds before next check...")
time.sleep(check_interval)
print(f"\nπŸ’₯ Space did not start after {max_checks} checks.")
print("πŸ”§ Check the HF Space logs for deployment errors.")
return False
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Monitor HF Space deployment')
parser.add_argument(
'--url',
default='https://ch404-cardserver.hf.space',
help='Base URL of the HF Space'
)
parser.add_argument(
'--interval',
type=int,
default=30,
help='Check interval in seconds'
)
parser.add_argument(
'--max-checks',
type=int,
default=20,
help='Maximum number of checks before giving up'
)
args = parser.parse_args()
success = monitor_space(args.url, args.interval, args.max_checks)
sys.exit(0 if success else 1)