mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-03 13:55:00 +08:00
严格检查全部宿主源码,清零类型错误预算并移除按模块类型豁免。 按原因、所属方法、错误码及语法指纹锁定必要兼容例外和站点类型桩, CI 阻断新增忽略、检查范围退化、本地桩替代真实接口及新的类型错误。 修复空值、异步和供应方类型边界,保持插件与 API 兼容合同, 并补充存储、订阅、通知、Agent 和工作流边界的回归覆盖。
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
"""
|
|
读取下载任务并写入工作流上下文。
|
|
"""
|
|
|
|
from app.runtime.log import logger
|
|
from app.runtime.stop import runtime_stop_state
|
|
from app.schemas.download import DownloadTask
|
|
from app.schemas.workflow import ActionContext, ActionParams
|
|
from app.workflow.actions import ActionChain, BaseAction
|
|
|
|
|
|
class FetchDownloadsParams(ActionParams):
|
|
"""
|
|
获取下载任务参数
|
|
"""
|
|
pass
|
|
|
|
|
|
class FetchDownloadsAction(BaseAction):
|
|
"""
|
|
获取下载任务
|
|
"""
|
|
|
|
contract = {
|
|
"inputs": [{"name": "downloads", "label": "下载任务", "kind": "list"}],
|
|
"outputs": [{"name": "downloads", "label": "下载任务", "kind": "list", "merge": "replace"}],
|
|
"concurrency_key": "download",
|
|
}
|
|
|
|
def __init__(self, action_id: str) -> None:
|
|
"""
|
|
初始化当前动作独立的执行状态。
|
|
"""
|
|
super().__init__(action_id)
|
|
self._downloads: list[DownloadTask] = []
|
|
|
|
name = "获取下载任务"
|
|
description = "获取下载队列中的任务状态"
|
|
data = FetchDownloadsParams().model_dump()
|
|
|
|
@property
|
|
def success(self) -> bool:
|
|
"""
|
|
返回当前动作是否已达到完成条件。
|
|
"""
|
|
return self.done
|
|
|
|
def execute(self, workflow_id: int, params: dict[str, object], context: ActionContext) -> ActionContext:
|
|
"""
|
|
更新downloads中的下载任务状态
|
|
"""
|
|
self._downloads = context.downloads or []
|
|
if not self._downloads:
|
|
self.job_done("无下载任务")
|
|
return context
|
|
|
|
for download in self._downloads:
|
|
if runtime_stop_state.is_workflow_stopped(workflow_id):
|
|
break
|
|
logger.info(f"获取下载任务 {download.download_id} 状态 ...")
|
|
if not download.download_id:
|
|
continue
|
|
torrents = ActionChain().list_torrents(
|
|
hashs=[download.download_id],
|
|
downloader=download.downloader,
|
|
)
|
|
if not torrents:
|
|
download.completed = True
|
|
continue
|
|
for t in torrents:
|
|
torrent_path = t.path
|
|
download.path = str(torrent_path) if torrent_path is not None else None
|
|
if t.progress is not None and t.progress >= 100:
|
|
logger.info(f"下载任务 {download.download_id} 已完成")
|
|
download.completed = True
|
|
else:
|
|
logger.info(f"下载任务 {download.download_id} 未完成")
|
|
download.completed = False
|
|
if all([d.completed for d in self._downloads]):
|
|
self.job_done("下载任务已全部完成")
|
|
return context
|