[fix] cleanup task to other thread
This commit is contained in:
parent
f5d13feaf9
commit
776b513858
3 changed files with 61 additions and 29 deletions
|
|
@ -102,34 +102,52 @@ class CleanupExpiredSandboxes:
|
|||
cleaned_sessions: list[SandboxSession] = []
|
||||
|
||||
for session in expired_sessions:
|
||||
with self._locker.lock(session.chat_id):
|
||||
current_session = self._repository.get_active_by_chat_id(
|
||||
session.chat_id
|
||||
)
|
||||
now = self._clock.now()
|
||||
if current_session is None:
|
||||
continue
|
||||
|
||||
if current_session.session_id != session.session_id:
|
||||
continue
|
||||
|
||||
if current_session.expires_at > now:
|
||||
continue
|
||||
|
||||
self._runtime.stop(current_session.container_id)
|
||||
self._repository.delete(current_session.session_id)
|
||||
cleaned_sessions.append(current_session)
|
||||
self._logger.info(
|
||||
'sandbox_cleaned',
|
||||
try:
|
||||
cleaned_session = self._cleanup_session(session)
|
||||
except Exception as exc:
|
||||
self._logger.error(
|
||||
'sandbox_clean_failed',
|
||||
attrs={
|
||||
'chat_id': current_session.chat_id,
|
||||
'session_id': current_session.session_id,
|
||||
'container_id': current_session.container_id,
|
||||
'chat_id': session.chat_id,
|
||||
'session_id': session.session_id,
|
||||
'container_id': session.container_id,
|
||||
'error': type(exc).__name__,
|
||||
},
|
||||
)
|
||||
continue
|
||||
|
||||
if cleaned_session is None:
|
||||
continue
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
return cleaned_sessions
|
||||
|
||||
def _cleanup_session(self, session: SandboxSession) -> SandboxSession | None:
|
||||
with self._locker.lock(session.chat_id):
|
||||
current_session = self._repository.get_active_by_chat_id(session.chat_id)
|
||||
now = self._clock.now()
|
||||
if current_session is None:
|
||||
return None
|
||||
|
||||
if current_session.session_id != session.session_id:
|
||||
return None
|
||||
|
||||
if current_session.expires_at > now:
|
||||
return None
|
||||
|
||||
self._runtime.stop(current_session.container_id)
|
||||
self._repository.delete(current_session.session_id)
|
||||
return current_session
|
||||
|
||||
|
||||
def _new_session_id() -> str:
|
||||
return uuid4().hex
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue