ref #6: [feat] add impl in memory session repository
This commit is contained in:
parent
87c789b7fe
commit
33ebcb1a82
4 changed files with 120 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from uuid import uuid4
|
||||
|
||||
from domain.sandbox import SandboxSession
|
||||
from usecase.interface import Clock, Logger, SandboxRuntime, SandboxSessionRepository
|
||||
|
|
@ -26,7 +27,47 @@ class CreateSandbox:
|
|||
self._ttl = ttl
|
||||
|
||||
def execute(self, command: CreateSandboxCommand) -> SandboxSession:
|
||||
raise NotImplementedError
|
||||
now = self._clock.now()
|
||||
session = self._repository.get_active_by_chat_id(command.chat_id)
|
||||
|
||||
if session is not None and session.expires_at > now:
|
||||
self._logger.info(
|
||||
'sandbox_reused',
|
||||
attrs={
|
||||
'chat_id': command.chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
},
|
||||
)
|
||||
return session
|
||||
|
||||
if session is not None:
|
||||
self._logger.info(
|
||||
'sandbox_replaced',
|
||||
attrs={
|
||||
'chat_id': command.chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
},
|
||||
)
|
||||
self._runtime.stop(session.container_id)
|
||||
self._repository.delete(session.session_id)
|
||||
|
||||
new_session = self._runtime.create(
|
||||
session_id=_new_session_id(),
|
||||
chat_id=command.chat_id,
|
||||
expires_at=now + self._ttl,
|
||||
)
|
||||
self._repository.save(new_session)
|
||||
self._logger.info(
|
||||
'sandbox_created',
|
||||
attrs={
|
||||
'chat_id': command.chat_id,
|
||||
'session_id': new_session.session_id,
|
||||
'container_id': new_session.container_id,
|
||||
},
|
||||
)
|
||||
return new_session
|
||||
|
||||
|
||||
class CleanupExpiredSandboxes:
|
||||
|
|
@ -44,3 +85,7 @@ class CleanupExpiredSandboxes:
|
|||
|
||||
def execute(self) -> list[SandboxSession]:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def _new_session_id() -> str:
|
||||
return uuid4().hex
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue