mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
refactor: move workflow execution writes to uow
This commit is contained in:
@@ -96,7 +96,7 @@ from app.db.oper.message import MessageOper
|
||||
from app.db.oper.subscribehistory import SubscribeHistoryOper
|
||||
from app.db.oper.plugindata import PluginDataOper
|
||||
from app.db.oper.systemconfig import SystemConfigOper
|
||||
from app.db.oper.workflow import WorkflowOper
|
||||
from app.db.oper.workflow import WorkflowOper, configure_workflow_legacy_writer
|
||||
from app.command import CommandChain
|
||||
from app.schemas.message import Message
|
||||
from app.schemas.message import MessageType
|
||||
@@ -113,6 +113,7 @@ from app.startup.subscription import (
|
||||
)
|
||||
from app.startup.chain_events import TransactionalChainDurableEventWriter
|
||||
from app.startup.download_failure import TransactionalDownloadFailureRepository
|
||||
from app.startup.workflow import TransactionalWorkflowExecutionService
|
||||
from app.startup.context import AgentChatRuntime, HostRuntime, SubscriptionRuntime
|
||||
from app.adapters.web.security.access import set_superuser_token_payload_provider
|
||||
from app.application.security.auth import build_superuser_token_payload
|
||||
@@ -570,6 +571,8 @@ async def init_modules() -> HostRuntime:
|
||||
configure_runtime_configuration(host_runtime.configuration)
|
||||
configure_api_data_runtime(host_runtime.compatibility_api_data)
|
||||
configure_runtime_data_providers()
|
||||
workflow_execution = TransactionalWorkflowExecutionService(SessionFactory)
|
||||
configure_workflow_legacy_writer(workflow_execution)
|
||||
configure_chain_data_ports(
|
||||
site=lambda: SiteOper(),
|
||||
subscribe=lambda: SubscribeOper(),
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""工作流执行状态事务适配器。"""
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Any, TypeVar
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.application.workflow import WorkflowExecutionCommand
|
||||
from app.db.oper.workflow import WorkflowOper
|
||||
from app.db.uow import SqlAlchemyUnitOfWork
|
||||
|
||||
|
||||
_Result = TypeVar("_Result")
|
||||
|
||||
|
||||
class TransactionalWorkflowExecutionService:
|
||||
"""为每次工作流执行状态写入创建独立短会话和 UnitOfWork。"""
|
||||
|
||||
def __init__(self, session_factory: Callable[[], Session]) -> None:
|
||||
"""保存由启动组合根提供的同步 Session 工厂。"""
|
||||
self._session_factory = session_factory
|
||||
|
||||
def start(self, workflow_id: int) -> bool:
|
||||
"""以独立事务提交运行中状态。"""
|
||||
return self._run(lambda command: command.start(workflow_id))
|
||||
|
||||
def success(self, workflow_id: int, result: str | None = None) -> bool:
|
||||
"""以独立事务提交成功状态。"""
|
||||
return self._run(lambda command: command.success(workflow_id, result))
|
||||
|
||||
def fail(self, workflow_id: int, result: str) -> bool:
|
||||
"""以独立事务提交失败状态。"""
|
||||
return self._run(lambda command: command.fail(workflow_id, result))
|
||||
|
||||
def step(
|
||||
self,
|
||||
workflow_id: int,
|
||||
action_id: str,
|
||||
context: dict[str, Any],
|
||||
execution_state: dict[str, Any] | None = None,
|
||||
) -> bool:
|
||||
"""以独立事务提交动作进度。"""
|
||||
return self._run(
|
||||
lambda command: command.step(
|
||||
workflow_id,
|
||||
action_id,
|
||||
context,
|
||||
execution_state,
|
||||
)
|
||||
)
|
||||
|
||||
def reset(self, workflow_id: int, reset_count: bool = False) -> bool:
|
||||
"""以独立事务提交执行状态重置。"""
|
||||
return self._run(
|
||||
lambda command: command.reset(workflow_id, reset_count)
|
||||
)
|
||||
|
||||
def _run(
|
||||
self,
|
||||
operation: Callable[[WorkflowExecutionCommand], _Result],
|
||||
) -> _Result:
|
||||
"""创建短会话并把提交/回滚交给 Application command。"""
|
||||
session = self._session_factory()
|
||||
try:
|
||||
command = WorkflowExecutionCommand(
|
||||
repository=WorkflowOper(db=session),
|
||||
unit_of_work=SqlAlchemyUnitOfWork(session),
|
||||
)
|
||||
return operation(command)
|
||||
finally:
|
||||
session.close()
|
||||
Reference in New Issue
Block a user