mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
refactor: migrate high-risk write transactions
This commit is contained in:
@@ -45,6 +45,14 @@ class AsyncAgentChatRepository(Protocol):
|
||||
"""删除指定服务端会话。"""
|
||||
...
|
||||
|
||||
async def async_stage_delete(
|
||||
self,
|
||||
session_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""暂存删除指定服务端会话,不提交调用方事务。"""
|
||||
...
|
||||
|
||||
def get(self, session_id: str, user_id: Optional[str] = None) -> Optional[Any]:
|
||||
"""同步读取服务端会话。"""
|
||||
...
|
||||
@@ -83,12 +91,29 @@ class AgentChatRecord:
|
||||
messages: list[dict]
|
||||
|
||||
|
||||
class AsyncUnitOfWork(Protocol):
|
||||
"""Agent 会话异步写用例所需的最小事务端口。"""
|
||||
|
||||
async def commit(self) -> None:
|
||||
"""提交当前请求事务。"""
|
||||
...
|
||||
|
||||
async def rollback(self) -> None:
|
||||
"""回滚当前请求事务。"""
|
||||
...
|
||||
|
||||
|
||||
class AgentChatService:
|
||||
"""统一执行 Agent 会话查询、访问控制和删除。"""
|
||||
|
||||
def __init__(self, repository: AsyncAgentChatRepository) -> None:
|
||||
"""保存异步会话持久化端口。"""
|
||||
def __init__(
|
||||
self,
|
||||
repository: AsyncAgentChatRepository,
|
||||
unit_of_work: Optional[AsyncUnitOfWork] = None,
|
||||
) -> None:
|
||||
"""保存会话持久化端口和可选请求级事务。"""
|
||||
self._repository = repository
|
||||
self._unit_of_work = unit_of_work
|
||||
|
||||
async def list(
|
||||
self,
|
||||
@@ -137,7 +162,18 @@ class AgentChatService:
|
||||
record = await self.get_accessible(session_id, principal)
|
||||
if record is None:
|
||||
return False
|
||||
return await self._repository.async_delete(session_id=session_id)
|
||||
if self._unit_of_work is None:
|
||||
return await self._repository.async_delete(session_id=session_id)
|
||||
try:
|
||||
deleted = await self._repository.async_stage_delete(
|
||||
session_id=session_id
|
||||
)
|
||||
if deleted:
|
||||
await self._unit_of_work.commit()
|
||||
return deleted
|
||||
except Exception:
|
||||
await self._unit_of_work.rollback()
|
||||
raise
|
||||
|
||||
def get_sync(self, session_id: str) -> Optional[AgentChatRecord]:
|
||||
"""同步读取会话投影,供同步 Agent 编排路径使用。"""
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
"""插件持久化数据写用例。"""
|
||||
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
class PluginDataMutationRepository(Protocol):
|
||||
"""插件数据删除命令所需的无提交仓储端口。"""
|
||||
|
||||
def stage_delete(self, plugin_id: str) -> None:
|
||||
"""暂存目标插件的全部持久化数据删除。"""
|
||||
...
|
||||
|
||||
|
||||
class UnitOfWork(Protocol):
|
||||
"""插件数据同步写用例所需的事务端口。"""
|
||||
|
||||
def commit(self) -> None:
|
||||
"""提交当前逻辑操作。"""
|
||||
...
|
||||
|
||||
def rollback(self) -> None:
|
||||
"""回滚当前逻辑操作。"""
|
||||
...
|
||||
|
||||
|
||||
class DeletePluginDataCommand:
|
||||
"""在一个显式事务中删除目标插件的全部持久化数据。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
repository: PluginDataMutationRepository,
|
||||
unit_of_work: UnitOfWork,
|
||||
) -> None:
|
||||
"""保存无提交仓储和事务所有者。"""
|
||||
self._repository = repository
|
||||
self._unit_of_work = unit_of_work
|
||||
|
||||
def execute(self, plugin_id: str) -> None:
|
||||
"""暂存并提交删除;任一步失败时回滚并传播原异常。"""
|
||||
try:
|
||||
self._repository.stage_delete(plugin_id)
|
||||
self._unit_of_work.commit()
|
||||
except Exception:
|
||||
self._unit_of_work.rollback()
|
||||
raise
|
||||
Reference in New Issue
Block a user