File size: 2,627 Bytes
45df059
 
 
4f94787
45df059
 
4f94787
45df059
4f94787
 
 
 
45df059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException
import logging
from core.redis_client import redis_client
from services.ollama_monitor import check_ollama_status
from utils.config import config
from utils.config_validator import ConfigValidator

logger = logging.getLogger(__name__)
router = APIRouter()

@router.get("/ollama-status")
async def get_ollama_status():
    """Returns the current status of the Ollama service."""
    try:
        status = check_ollama_status()
        return status
    except Exception as e:
        logger.error(f"Error checking Ollama status: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/health")
async def health_check():
    """Comprehensive health check for all services"""
    try:
        # Check configuration
        validator = ConfigValidator()
        config_report = validator.validate_all()
        
        # Check Redis
        redis_healthy = redis_client.is_healthy() if redis_client.get_client() else False
        
        # Check Ollama
        try:
            ollama_status = check_ollama_status()
            ollama_healthy = ollama_status.get("running", False)
        except Exception as e:
            logger.error(f"Error checking Ollama status: {e}")
            ollama_healthy = False
            ollama_status = {"error": str(e)}
        
        # Overall health
        overall_healthy = redis_healthy and ollama_healthy and config_report['valid']
        
        return {
            "status": "healthy" if overall_healthy else "degraded",
            "services": {
                "redis": "healthy" if redis_healthy else "unhealthy",
                "ollama": "healthy" if ollama_healthy else "unhealthy",
                "configuration": "valid" if config_report['valid'] else "invalid"
            },
            "details": {
                "redis_healthy": redis_healthy,
                "ollama_status": ollama_status,
                "config_validation": config_report
            }
        }
    except Exception as e:
        logger.error(f"Health check failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/config")
async def config_status():
    """Return current configuration status"""
    validator = ConfigValidator()
    report = validator.validate_all()
    
    return {
        "configuration": {
            "ollama_host": config.ollama_host,
            "hf_api_url": config.hf_api_url,
            "redis_host": config.redis_host,
            "redis_port": config.redis_port,
            "use_fallback": config.use_fallback
        },
        "validation": report
    }