File size: 3,692 Bytes
d9ce58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4fbe2e
d9ce58c
 
 
 
b4fbe2e
 
d9ce58c
b4fbe2e
 
 
 
 
 
 
d9ce58c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import asyncio
from fastapi import APIRouter, HTTPException
from langchain.schema import SystemMessage
from api.models import HealthCheckResponse
from services.mistral_client import MistralClient
from services.flux_client import FluxClient

def get_health_router(mistral_client: MistralClient, flux_client: FluxClient) -> APIRouter:
    router = APIRouter()

    @router.get("/health/mistral", response_model=HealthCheckResponse)
    async def check_mistral_health():
        """Vérifie la disponibilité du service Mistral."""
        print("Checking Mistral health...")
        start_time = time.time()
        try:
            # Try to make a simple request to Mistral with a 5 second timeout
            await asyncio.wait_for(
                mistral_client.check_health(),
                timeout=5.0
            )
            
            latency = (time.time() - start_time) * 1000  # Convert to milliseconds
            print(f"Mistral health check successful. Latency: {latency}ms")
            return HealthCheckResponse(
                status="healthy",
                service="mistral",
                latency=latency
            )
        except asyncio.TimeoutError:
            print("Mistral health check failed: timeout")
            raise HTTPException(
                status_code=503,
                detail=HealthCheckResponse(
                    status="unhealthy",
                    service="mistral",
                    latency=None,
                    error="Request timed out after 5 seconds"
                ).dict()
            )
        except Exception as e:
            print(f"Mistral health check failed: {str(e)}")
            raise HTTPException(
                status_code=503,
                detail=HealthCheckResponse(
                    status="unhealthy",
                    service="mistral",
                    latency=None,
                    error=str(e)
                ).dict()
            )

    @router.get("/health/flux", response_model=HealthCheckResponse)
    async def check_flux_health():
        """Vérifie la disponibilité du service Flux."""
        print("Checking Flux health...")
        start_time = time.time()
        try:
            # Try to generate a test image with a timeout
            is_healthy = await asyncio.wait_for(
                flux_client.check_health(),
                timeout=5.0  # Même timeout que Mistral
            )
            
            if not is_healthy:
                raise Exception("Failed to generate test image")
                
            latency = (time.time() - start_time) * 1000  # Convert to milliseconds
            print(f"Flux health check successful. Latency: {latency}ms")
            return HealthCheckResponse(
                status="healthy",
                service="flux",
                latency=latency
            )
        except asyncio.TimeoutError:
            print("Flux health check failed: timeout")
            raise HTTPException(
                status_code=503,
                detail=HealthCheckResponse(
                    status="unhealthy",
                    service="flux",
                    latency=None,
                    error="Image generation timed out after 5 seconds"
                ).dict()
            )
        except Exception as e:
            print(f"Flux health check failed: {str(e)}")
            raise HTTPException(
                status_code=503,
                detail=HealthCheckResponse(
                    status="unhealthy",
                    service="flux",
                    latency=None,
                    error=str(e)
                ).dict()
            )

    return router