[fix] restart gap
This commit is contained in:
parent
770af1fe76
commit
50af62b3fb
10 changed files with 348 additions and 4 deletions
39
adapter/sandbox/reconciliation.py
Normal file
39
adapter/sandbox/reconciliation.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Protocol
|
||||
from uuid import UUID
|
||||
|
||||
from domain.sandbox import SandboxSession
|
||||
from usecase.interface import Logger
|
||||
|
||||
|
||||
class SandboxSessionStateSource(Protocol):
|
||||
def list_active_sessions(self) -> list[SandboxSession]: ...
|
||||
|
||||
|
||||
class SandboxSessionRegistry(Protocol):
|
||||
def replace_all(self, sessions: list[SandboxSession]) -> None: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SandboxSessionReconciler:
|
||||
state_source: SandboxSessionStateSource
|
||||
registry: SandboxSessionRegistry
|
||||
logger: Logger
|
||||
|
||||
def execute(self) -> list[SandboxSession]:
|
||||
sessions_by_chat_id: dict[UUID, SandboxSession] = {}
|
||||
for session in sorted(
|
||||
self.state_source.list_active_sessions(),
|
||||
key=lambda item: item.created_at,
|
||||
):
|
||||
sessions_by_chat_id[session.chat_id] = session
|
||||
|
||||
sessions = list(sessions_by_chat_id.values())
|
||||
self.registry.replace_all(sessions)
|
||||
self.logger.info(
|
||||
'sandbox_reconciled',
|
||||
attrs={
|
||||
'session_count': len(sessions),
|
||||
},
|
||||
)
|
||||
return sessions
|
||||
Loading…
Add table
Add a link
Reference in a new issue