Buckets:

download
raw
4.94 kB
"""
ACGS-2 Circuit Breaker FastAPI Router
Constitutional Hash: 608508a9bd224290
This module provides FastAPI endpoints for circuit breaker health monitoring.
"""
from datetime import UTC, datetime
from fastapi import APIRouter
from fastapi.responses import JSONResponse
try:
from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH
except ImportError:
CONSTITUTIONAL_HASH = "standalone"
from enhanced_agent_bus.observability.structured_logging import get_logger
from .enums import CircuitState
from .registry import get_circuit_breaker_registry
logger = get_logger(__name__)
def create_circuit_health_router() -> APIRouter | None:
"""Create FastAPI router for circuit breaker health endpoints."""
try:
router = APIRouter(prefix="/health", tags=["Health"])
async def get_circuit_states() -> JSONResponse:
"""
Get status of all circuit breakers.
Returns:
JSONResponse with circuit breaker states and health summary
"""
registry = get_circuit_breaker_registry()
# Initialize default circuits if not already done
await registry.initialize_default_circuits()
summary = registry.get_health_summary()
states = registry.get_all_states()
return JSONResponse(
content={
"summary": summary,
"circuits": states,
},
status_code=200 if summary["status"] != "critical" else 503,
)
async def get_circuit_state(service_name: str) -> JSONResponse:
"""
Get status of a specific circuit breaker.
Args:
service_name: Name of the service
Returns:
JSONResponse with circuit breaker status
"""
registry = get_circuit_breaker_registry()
cb = registry.get(service_name)
if cb is None:
return JSONResponse(
content={
"error": f"Circuit breaker '{service_name}' not found",
"constitutional_hash": CONSTITUTIONAL_HASH,
},
status_code=404,
)
status = cb.get_status()
return JSONResponse(
content=status,
status_code=200 if cb.state != CircuitState.OPEN else 503,
)
async def reset_circuit(service_name: str) -> JSONResponse:
"""
Reset a specific circuit breaker to closed state.
Args:
service_name: Name of the service
Returns:
JSONResponse with reset confirmation
"""
registry = get_circuit_breaker_registry()
success = await registry.reset(service_name)
if not success:
return JSONResponse(
content={
"error": f"Circuit breaker '{service_name}' not found",
"constitutional_hash": CONSTITUTIONAL_HASH,
},
status_code=404,
)
return JSONResponse(
content={
"service": service_name,
"action": "reset",
"new_state": "closed",
"constitutional_hash": CONSTITUTIONAL_HASH,
"timestamp": datetime.now(UTC).isoformat(),
},
status_code=200,
)
async def reset_all_circuits() -> JSONResponse:
"""
Reset all circuit breakers to closed state.
Returns:
JSONResponse with reset confirmation
"""
registry = get_circuit_breaker_registry()
await registry.reset_all()
return JSONResponse(
content={
"action": "reset_all",
"circuits_reset": list(registry.get_all_states().keys()),
"constitutional_hash": CONSTITUTIONAL_HASH,
"timestamp": datetime.now(UTC).isoformat(),
},
status_code=200,
)
router.add_api_route("/circuits", get_circuit_states, methods=["GET"])
router.add_api_route("/circuits/{service_name}", get_circuit_state, methods=["GET"])
router.add_api_route(
"/circuits/{service_name}/reset",
reset_circuit,
methods=["POST"],
)
router.add_api_route("/circuits/reset-all", reset_all_circuits, methods=["POST"])
return router
except ImportError:
logger.warning(
f"[{CONSTITUTIONAL_HASH}] FastAPI not available, circuit health router not created"
)
return None
__all__ = [
"create_circuit_health_router",
]

Xet Storage Details

Size:
4.94 kB
·
Xet hash:
c81e32814760df926f2ae9f0a3930da6de0f113ffd290cfb1d33ef201639b9d4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.