[fix] restart gap
This commit is contained in:
parent
770af1fe76
commit
50af62b3fb
10 changed files with 348 additions and 4 deletions
|
|
@ -11,6 +11,8 @@ from domain.error import SandboxError, SandboxStartError
|
|||
from domain.sandbox import SandboxSession, SandboxStatus
|
||||
from usecase.interface import SandboxRuntime
|
||||
|
||||
SANDBOX_LABELS = ('session_id', 'chat_id', 'expires_at')
|
||||
|
||||
|
||||
class DockerSandboxRuntime(SandboxRuntime):
|
||||
def __init__(
|
||||
|
|
@ -69,6 +71,23 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
except DockerException as exc:
|
||||
raise SandboxError('sandbox_stop_failed') from exc
|
||||
|
||||
def list_active_sessions(self) -> list[SandboxSession]:
|
||||
try:
|
||||
containers = self._client.containers.list(
|
||||
filters={'label': list(SANDBOX_LABELS)}
|
||||
)
|
||||
except DockerException as exc:
|
||||
raise SandboxError('sandbox_list_failed') from exc
|
||||
|
||||
sessions: list[SandboxSession] = []
|
||||
for container in containers:
|
||||
session = self._session_from_container(container)
|
||||
if session is None:
|
||||
continue
|
||||
sessions.append(session)
|
||||
|
||||
return sessions
|
||||
|
||||
def _labels(
|
||||
self,
|
||||
session_id: UUID,
|
||||
|
|
@ -120,5 +139,50 @@ class DockerSandboxRuntime(SandboxRuntime):
|
|||
raise ValueError('invalid host path')
|
||||
return host_path
|
||||
|
||||
def _session_from_container(self, container: object) -> SandboxSession | None:
|
||||
container_id = str(getattr(container, 'id', '')).strip()
|
||||
labels = getattr(container, 'labels', None)
|
||||
if not container_id or not isinstance(labels, dict):
|
||||
return None
|
||||
|
||||
try:
|
||||
session_id = UUID(labels['session_id'])
|
||||
chat_id = UUID(labels['chat_id'])
|
||||
created_at = self._container_created_at(container)
|
||||
expires_at = _parse_datetime(labels['expires_at'])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
return None
|
||||
|
||||
return SandboxSession(
|
||||
session_id=session_id,
|
||||
chat_id=chat_id,
|
||||
container_id=container_id,
|
||||
status=SandboxStatus.RUNNING,
|
||||
created_at=created_at,
|
||||
expires_at=expires_at,
|
||||
)
|
||||
|
||||
def _container_created_at(self, container: object) -> datetime:
|
||||
attrs = getattr(container, 'attrs', None)
|
||||
if not isinstance(attrs, dict):
|
||||
reload_container = getattr(container, 'reload', None)
|
||||
if callable(reload_container):
|
||||
reload_container()
|
||||
attrs = getattr(container, 'attrs', None)
|
||||
|
||||
if not isinstance(attrs, dict):
|
||||
raise ValueError('invalid container attrs')
|
||||
|
||||
raw_created_at = attrs.get('Created')
|
||||
if not isinstance(raw_created_at, str):
|
||||
raise ValueError('invalid created_at')
|
||||
|
||||
return _parse_datetime(raw_created_at)
|
||||
|
||||
def _host_path(self, path_value: str) -> Path:
|
||||
return Path(path_value).expanduser().resolve(strict=False)
|
||||
|
||||
|
||||
def _parse_datetime(value: str) -> datetime:
|
||||
normalized = f'{value[:-1]}+00:00' if value.endswith('Z') else value
|
||||
return datetime.fromisoformat(normalized)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue