Clover/health.py

92 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Health check endpoint for Clover service.
This script can be called directly to check the health status of the Clover service.
"""
import json
import sys
import os
import datetime
# Add the current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def check_health():
"""Check the health status of the Clover service"""
try:
# Import required modules
from config.settings import load_config
from tools.command_safety import get_command_status
from tools.resource_monitor import get_resource_status
# Load configuration
config = load_config()
# Check basic service status
service_status = {
"status": "healthy",
"service": "clover_ai",
"version": "1.0.0",
"config_loaded": True,
"timestamp": datetime.datetime.now().isoformat()
}
# Check safety systems
try:
safety_status = get_command_status()
service_status["safety"] = {
"safety_available": safety_status.get("safety_available", False),
"violations": safety_status.get("safety", {}).get("violation_count", 0)
}
except Exception as e:
service_status["safety"] = {"error": str(e)}
# Check resource usage
try:
resource_status = get_resource_status()
service_status["resources"] = {
"cpu_percent": resource_status.get("current_resources", {}).get("cpu_percent", 0),
"memory_mb": resource_status.get("current_resources", {}).get("memory_mb", 0),
"processes": resource_status.get("current_resources", {}).get("process_count", 0)
}
except Exception as e:
service_status["resources"] = {"error": str(e)}
# Check if main components are accessible
try:
from models.model_manager import ModelManager
model_manager = ModelManager()
service_status["model_manager"] = {
"initialized": True,
"available_models": len(model_manager.get_available_models())
}
except Exception as e:
service_status["model_manager"] = {"error": str(e)}
return service_status
except Exception as e:
error_status = {
"status": "unhealthy",
"error": str(e),
"timestamp": datetime.datetime.now().isoformat()
}
return error_status
def main():
"""Main health check function"""
status = check_health()
# Print JSON response
print(json.dumps(status, indent=2))
# Exit with appropriate code
if status.get("status") == "healthy":
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
main()