decomposer: generate deliverable files for Define the service contract and project architecture for the FastAPI endpoint monitoring service.; Implement the typed monitor CRUD API and concurrency-safe in-memory state according to the service design.; Implement secure on-demand endpoint checks with status updates, latency measurement, robust error handling, and redacted structured logs.; Add operational API endpoints and environment-driven runtime configuration to the monitoring service.; Create automated tests for the monitoring service.; Package the service with Docker and developer documentation.; Validate the complete project.
Some checks failed
ci / validate (push) Has been cancelled

This commit is contained in:
2026-08-09 15:46:43 +00:00
parent d31ef63b2a
commit 7b2eaa0dd2

View File

@@ -1,65 +1,53 @@
import asyncio import asyncio
from datetime import UTC, datetime
from uuid import UUID from uuid import UUID
from app.models import CurrentStatus, Monitor, MonitorCreate, MonitorUpdate, now_utc from app.models import CurrentStatus, Monitor, MonitorCreate, MonitorUpdate
class MonitorNotFoundError(KeyError):
pass
class MonitorStore: class MonitorStore:
"""Concurrency-safe, process-local monitor storage. """Concurrency-safe process-local state; all values cross the lock as copies."""
Models are frozen and every read returns an immutable value, so callers cannot mutate
state outside the lock. All read-modify-write operations happen under one asyncio lock.
"""
def __init__(self) -> None: def __init__(self) -> None:
self._items: dict[UUID, Monitor] = {} self._items: dict[UUID, Monitor] = {}
self._lock = asyncio.Lock() self._lock = asyncio.Lock()
async def create(self, data: MonitorCreate) -> Monitor: async def create(self, data: MonitorCreate) -> Monitor:
monitor = Monitor(**data.model_dump()) monitor = Monitor(name=data.name, url=data.url)
async with self._lock: async with self._lock:
self._items[monitor.id] = monitor self._items[monitor.id] = monitor
return monitor return monitor.model_copy(deep=True)
async def list(self) -> list[Monitor]: async def list(self) -> list[Monitor]:
async with self._lock: async with self._lock:
return list(self._items.values()) return [item.model_copy(deep=True) for item in self._items.values()]
async def get(self, monitor_id: UUID) -> Monitor: async def get(self, monitor_id: UUID) -> Monitor | None:
async with self._lock: async with self._lock:
try: item = self._items.get(monitor_id)
return self._items[monitor_id] return item.model_copy(deep=True) if item else None
except KeyError as exc:
raise MonitorNotFoundError(monitor_id) from exc
async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor: async def update(self, monitor_id: UUID, data: MonitorUpdate) -> Monitor | None:
changes = data.model_dump(exclude_unset=True, exclude_none=True) changes = data.model_dump(exclude_unset=True, exclude_none=True)
async with self._lock: async with self._lock:
try: item = self._items.get(monitor_id)
existing = self._items[monitor_id] if item is None:
except KeyError as exc: return None
raise MonitorNotFoundError(monitor_id) from exc updated = item.model_copy(update={**changes, "updated_at": datetime.now(UTC)})
updated = existing.model_copy(update={**changes, "updated_at": now_utc()})
self._items[monitor_id] = updated self._items[monitor_id] = updated
return updated return updated.model_copy(deep=True)
async def delete(self, monitor_id: UUID) -> None: async def delete(self, monitor_id: UUID) -> bool:
async with self._lock: async with self._lock:
if self._items.pop(monitor_id, None) is None: return self._items.pop(monitor_id, None) is not None
raise MonitorNotFoundError(monitor_id)
async def set_status(self, monitor_id: UUID, status: CurrentStatus) -> Monitor: async def set_status(self, monitor_id: UUID, status: CurrentStatus) -> Monitor | None:
async with self._lock: async with self._lock:
try: item = self._items.get(monitor_id)
existing = self._items[monitor_id] if item is None:
except KeyError as exc: return None
raise MonitorNotFoundError(monitor_id) from exc updated = item.model_copy(
updated = existing.model_copy( update={"current_status": status, "updated_at": datetime.now(UTC)}
update={"current_status": status, "updated_at": now_utc()}
) )
self._items[monitor_id] = updated self._items[monitor_id] = updated
return updated return updated.model_copy(deep=True)