mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
refactor: unify workflow runtime boundary
This commit is contained in:
@@ -14,9 +14,9 @@ from app.application.workflow import (
|
||||
WorkflowDefinitionCommand,
|
||||
WorkflowMutationCommand,
|
||||
WorkflowQueryService,
|
||||
get_workflow_manager,
|
||||
)
|
||||
from app.runtime.config import global_vars
|
||||
from app.workflow import WorkFlowManager
|
||||
from app.startup.composition.context import HostRuntime
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ def get_workflow_mutation_command(
|
||||
) -> WorkflowMutationCommand:
|
||||
"""组装请求级工作流写用例和提交后的调度副作用。"""
|
||||
scheduler = Scheduler()
|
||||
workflow_manager = WorkFlowManager()
|
||||
workflow_manager = get_workflow_manager()
|
||||
system_config = cast(WorkflowCachePort, runtime.workflow.system_config())
|
||||
return WorkflowMutationCommand(
|
||||
repository=runtime.workflow.repository(db),
|
||||
|
||||
@@ -13,10 +13,10 @@ from app.application.workflow import (
|
||||
WorkflowDefinitionCommand,
|
||||
WorkflowMutationCommand,
|
||||
WorkflowQueryService,
|
||||
get_workflow_manager,
|
||||
)
|
||||
from app.chain.workflow import WorkflowChain
|
||||
from app.application.plugin.runtime import get_plugin_manager as PluginManager
|
||||
from app.workflow import WorkFlowManager
|
||||
from app.api.dependencies.auth import (
|
||||
get_current_active_manage_user,
|
||||
get_current_active_manage_user_async,
|
||||
@@ -78,7 +78,7 @@ async def list_actions(_: Any = Depends(get_current_active_manage_user_async)) -
|
||||
"""
|
||||
获取所有动作
|
||||
"""
|
||||
return WorkFlowManager().list_actions()
|
||||
return get_workflow_manager().list_actions()
|
||||
|
||||
|
||||
@router.get(
|
||||
|
||||
@@ -17,6 +17,56 @@ SUPPORTED_WORKFLOW_TRIGGERS = {
|
||||
}
|
||||
|
||||
|
||||
class WorkflowRuntime(Protocol):
|
||||
"""声明宿主入口与 Chain 消费的工作流运行时能力。"""
|
||||
|
||||
def execute(self, *args: Any, **kwargs: Any) -> Any:
|
||||
"""执行单个工作流动作,参数与 concrete 管理器保持一致。"""
|
||||
...
|
||||
|
||||
def list_actions(self) -> list[dict[str, Any]]:
|
||||
"""返回当前运行时登记的工作流动作定义。"""
|
||||
...
|
||||
|
||||
def load_workflow_events(self, workflow_id: Optional[int] = None) -> None:
|
||||
"""加载全部或指定工作流的事件触发器。"""
|
||||
...
|
||||
|
||||
def remove_workflow_event(
|
||||
self,
|
||||
workflow_id: Optional[int] = None,
|
||||
event_type_str: Optional[str] = None,
|
||||
) -> None:
|
||||
"""移除全部或指定工作流的事件触发器。"""
|
||||
...
|
||||
|
||||
def update_workflow_event(self, workflow: Any) -> None:
|
||||
"""按最新定义刷新工作流事件触发器。"""
|
||||
...
|
||||
|
||||
|
||||
WorkflowRuntimeProvider = Callable[[], WorkflowRuntime]
|
||||
|
||||
|
||||
def _unconfigured_workflow_runtime() -> WorkflowRuntime:
|
||||
"""拒绝在启动组合根装配前隐式创建工作流管理器。"""
|
||||
raise RuntimeError("工作流运行时尚未由启动组合根装配")
|
||||
|
||||
|
||||
_workflow_runtime_provider: WorkflowRuntimeProvider = _unconfigured_workflow_runtime
|
||||
|
||||
|
||||
def configure_workflow_runtime(provider: WorkflowRuntimeProvider) -> None:
|
||||
"""由启动组合根登记工作流运行时实例提供器。"""
|
||||
global _workflow_runtime_provider
|
||||
_workflow_runtime_provider = provider
|
||||
|
||||
|
||||
def get_workflow_manager() -> WorkflowRuntime:
|
||||
"""返回组合根提供的工作流运行时,避免消费者直接定位 Singleton。"""
|
||||
return _workflow_runtime_provider()
|
||||
|
||||
|
||||
class AsyncWorkflowQueryRepository(Protocol):
|
||||
"""工作流查询用例需要的异步读取端口。"""
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from pydantic import BaseModel
|
||||
from app.chain import ChainBase
|
||||
from app.runtime.config import global_vars
|
||||
from app.runtime.events import Event, eventmanager
|
||||
from app.application.workflow import get_workflow_manager
|
||||
from app.application.chain.data import WorkflowPortProxy as WorkflowOper
|
||||
from app.runtime.log import logger
|
||||
from app.schemas.workflow import ActionContext
|
||||
@@ -25,7 +26,6 @@ from app.schemas.workflow import Action
|
||||
from app.schemas.workflow import ActionExecution
|
||||
from app.schemas.workflow import ActionResult
|
||||
from app.schemas.types import EventType
|
||||
from app.workflow import WorkFlowManager
|
||||
|
||||
ARTIFACT_FIELDS = {"torrents", "medias", "fileitems", "downloads", "sites", "subscribes"}
|
||||
DEFAULT_WORKFLOW_MAX_WORKERS = 4
|
||||
@@ -162,7 +162,7 @@ class WorkflowExecutor:
|
||||
self.flow_failed = set()
|
||||
|
||||
# 工作流管理器
|
||||
self.workflowmanager = WorkFlowManager()
|
||||
self.workflowmanager = get_workflow_manager()
|
||||
# 线程安全队列
|
||||
self.queue = deque()
|
||||
self.queued_actions = set()
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
from app.application.workflow import configure_workflow_runtime
|
||||
from app.workflow import WorkFlowManager
|
||||
|
||||
|
||||
# 启动模块是 concrete WorkFlowManager 的唯一宿主装配边界。
|
||||
configure_workflow_runtime(lambda: WorkFlowManager())
|
||||
|
||||
|
||||
def init_workflow():
|
||||
"""
|
||||
初始化工作流
|
||||
|
||||
Reference in New Issue
Block a user