PLBA2-2. Реализация

This commit is contained in:
2026-10-10 19:36:32 +03:00
parent 473cde126f
commit 71998a6b13
120 changed files with 3675 additions and 21 deletions
+81
View File
@@ -0,0 +1,81 @@
from typing import Any
from plba.health.aggregator import HealthAggregator, HealthInputInvalid
from plba.http.asgi import ASGIHttpTransport
from plba.http.errors import ControlRequestError
from plba.http.requests import StopRequest
from plba.http.serialization import health_report_payload
from plba.runtime.application import ApplicationRuntime, RuntimeLifecycleError
class ControlApi:
"""ASGI route adapter; it owns no server, socket, or listener."""
def __init__(
self, runtime: ApplicationRuntime, health_aggregator: HealthAggregator
) -> None:
self._runtime = runtime
self._health_aggregator = health_aggregator
self._transport = ASGIHttpTransport()
async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None:
scope_type = scope.get("type")
if scope_type == "lifespan":
await self._transport.handle_lifespan(receive, send)
return
if scope_type != "http":
return
try:
status, payload = await self._dispatch(scope, receive)
except HealthInputInvalid as error:
status, payload = 500, self._error("HEALTH_INPUT_INVALID", error)
except RuntimeLifecycleError as error:
status, payload = 409, self._error("APPLICATION_CONTROL_CONFLICT", error)
except ControlRequestError as error:
status, payload = 400, self._error("HTTP_CONTROL_REJECTED", error)
except Exception as error:
status, payload = 500, self._error("CONTROL_API_FAILED", error)
await self._transport.send_json(send, status, payload)
async def _dispatch(self, scope: dict[str, Any], receive: Any) -> tuple[int, dict[str, Any]]:
method = scope.get("method", "").upper()
path = scope.get("path", "")
query = scope.get("query_string", b"")
if path in {"/health", "/runtime/status", "/runtime/start", "/runtime/stop"} and query:
raise ControlRequestError("query parameters are not accepted")
if path == "/health":
if method != "GET":
return 405, self._error("METHOD_NOT_ALLOWED", "GET required")
await self._require_empty_body(receive)
report = await self._health_aggregator.aggregate()
return 200, health_report_payload(report)
if path == "/runtime/start":
if method != "POST":
return 405, self._error("METHOD_NOT_ALLOWED", "POST required")
await self._require_empty_body(receive)
result = await self._runtime.start()
return 200, {"status": result.lifecycle.value}
if path == "/runtime/status":
if method != "GET":
return 405, self._error("METHOD_NOT_ALLOWED", "GET required")
await self._require_empty_body(receive)
return 200, {"status": self._runtime.lifecycle.value}
if path == "/runtime/stop":
if method != "POST":
return 405, self._error("METHOD_NOT_ALLOWED", "POST required")
request = StopRequest.parse(await self._transport.read_body(receive))
result = await self._runtime.stop(
force=request.force, timeout_seconds=request.timeout_seconds
)
status_code = 200 if result.lifecycle.value == "stopped" else 202
return status_code, {"status": result.lifecycle.value}
return 404, self._error("NOT_FOUND", "control route not found")
async def _require_empty_body(self, receive: Any) -> None:
if await self._transport.read_body(receive):
raise ControlRequestError("request body is not accepted")
@staticmethod
def _error(code: str, error: Exception | str) -> dict[str, str]:
return {"error": code, "message": str(error)}