[feat] change str id type to UUID
This commit is contained in:
parent
e629e34c4d
commit
770af1fe76
11 changed files with 150 additions and 173 deletions
|
|
@ -2,6 +2,7 @@ from collections.abc import Mapping
|
|||
from datetime import datetime
|
||||
from types import TracebackType
|
||||
from typing import Protocol, TypeAlias
|
||||
from uuid import UUID
|
||||
|
||||
from domain.sandbox import SandboxSession
|
||||
from domain.user import User
|
||||
|
|
@ -19,13 +20,13 @@ class UserRepository(Protocol):
|
|||
|
||||
|
||||
class SandboxSessionRepository(Protocol):
|
||||
def get_active_by_chat_id(self, chat_id: str) -> SandboxSession | None: ...
|
||||
def get_active_by_chat_id(self, chat_id: UUID) -> SandboxSession | None: ...
|
||||
|
||||
def list_expired(self, now: datetime) -> list[SandboxSession]: ...
|
||||
|
||||
def save(self, session: SandboxSession) -> None: ...
|
||||
|
||||
def delete(self, session_id: str) -> None: ...
|
||||
def delete(self, session_id: UUID) -> None: ...
|
||||
|
||||
|
||||
class LockContext(Protocol):
|
||||
|
|
@ -40,15 +41,15 @@ class LockContext(Protocol):
|
|||
|
||||
|
||||
class SandboxLifecycleLocker(Protocol):
|
||||
def lock(self, chat_id: str) -> LockContext: ...
|
||||
def lock(self, chat_id: UUID) -> LockContext: ...
|
||||
|
||||
|
||||
class SandboxRuntime(Protocol):
|
||||
def create(
|
||||
self,
|
||||
*,
|
||||
session_id: str,
|
||||
chat_id: str,
|
||||
session_id: UUID,
|
||||
chat_id: UUID,
|
||||
created_at: datetime,
|
||||
expires_at: datetime,
|
||||
) -> SandboxSession: ...
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ from usecase.interface import (
|
|||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CreateSandboxCommand:
|
||||
chat_id: str
|
||||
chat_id: UUID
|
||||
|
||||
|
||||
class CreateSandbox:
|
||||
|
|
@ -35,7 +35,7 @@ class CreateSandbox:
|
|||
self._ttl = ttl
|
||||
|
||||
def execute(self, command: CreateSandboxCommand) -> SandboxSession:
|
||||
chat_id = _canonical_chat_id(command.chat_id)
|
||||
chat_id = command.chat_id
|
||||
|
||||
with self._locker.lock(chat_id):
|
||||
session = self._repository.get_active_by_chat_id(chat_id)
|
||||
|
|
@ -44,22 +44,14 @@ class CreateSandbox:
|
|||
if session is not None and session.expires_at > now:
|
||||
self._logger.info(
|
||||
'sandbox_reused',
|
||||
attrs={
|
||||
'chat_id': chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
},
|
||||
attrs=_sandbox_attrs(session),
|
||||
)
|
||||
return session
|
||||
|
||||
if session is not None:
|
||||
self._logger.info(
|
||||
'sandbox_replaced',
|
||||
attrs={
|
||||
'chat_id': chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
},
|
||||
attrs=_sandbox_attrs(session),
|
||||
)
|
||||
self._runtime.stop(session.container_id)
|
||||
self._repository.delete(session.session_id)
|
||||
|
|
@ -75,11 +67,7 @@ class CreateSandbox:
|
|||
self._repository.save(new_session)
|
||||
self._logger.info(
|
||||
'sandbox_created',
|
||||
attrs={
|
||||
'chat_id': chat_id,
|
||||
'session_id': new_session.session_id,
|
||||
'container_id': new_session.container_id,
|
||||
},
|
||||
attrs=_sandbox_attrs(new_session),
|
||||
)
|
||||
return new_session
|
||||
|
||||
|
|
@ -107,14 +95,11 @@ class CleanupExpiredSandboxes:
|
|||
try:
|
||||
cleaned_session = self._cleanup_session(session)
|
||||
except Exception as exc:
|
||||
attrs = _sandbox_attrs(session)
|
||||
attrs['error'] = type(exc).__name__
|
||||
self._logger.error(
|
||||
'sandbox_clean_failed',
|
||||
attrs={
|
||||
'chat_id': session.chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
'error': type(exc).__name__,
|
||||
},
|
||||
attrs=attrs,
|
||||
)
|
||||
continue
|
||||
|
||||
|
|
@ -124,11 +109,7 @@ class CleanupExpiredSandboxes:
|
|||
cleaned_sessions.append(cleaned_session)
|
||||
self._logger.info(
|
||||
'sandbox_cleaned',
|
||||
attrs={
|
||||
'chat_id': cleaned_session.chat_id,
|
||||
'session_id': cleaned_session.session_id,
|
||||
'container_id': cleaned_session.container_id,
|
||||
},
|
||||
attrs=_sandbox_attrs(cleaned_session),
|
||||
)
|
||||
|
||||
return cleaned_sessions
|
||||
|
|
@ -151,9 +132,13 @@ class CleanupExpiredSandboxes:
|
|||
return current_session
|
||||
|
||||
|
||||
def _new_session_id() -> str:
|
||||
return uuid4().hex
|
||||
def _new_session_id() -> UUID:
|
||||
return uuid4()
|
||||
|
||||
|
||||
def _canonical_chat_id(chat_id: str) -> str:
|
||||
return str(UUID(str(chat_id).strip()))
|
||||
def _sandbox_attrs(session: SandboxSession) -> dict[str, str]:
|
||||
return {
|
||||
'chat_id': str(session.chat_id),
|
||||
'session_id': str(session.session_id),
|
||||
'container_id': session.container_id,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue