Archived
## uv 环境管理 - pyproject.toml 声明 dependencies + [dependency-groups].dev,uv.lock 精确锁定 - 删除 requirements.txt / requirements-fastapi.txt / requirements-dev.txt - .python-version = 3.12;Dockerfile 改用 uv sync --frozen(可复现构建) - 新增 jinja2(首页渲染) ## 删除 Flask 技术债 - 从 main.py 用 ast 精确剥离 56 个 Flask 节点(路由/中间件/错误处理/启动), 框架无关的服务层落为 services/runtime.py(MusicAPIService、下载/打包 worker、 任务监控、缓存清扫),零 Flask 依赖 - 删除 main.py(4527 行)与 auth_routes.py(1050 行) - auth_system.py 移除会话/请求上下文层(login_session/current_user/login_required/ admin_required/_client_ip 等),保留数据与业务层 - 新增 core/net.py(IP 解析)、web/auth_state.py(登录锁定/2FA 票据/发码限流) - FastAPI 侧改直接依赖 services.runtime,不再 import main - web/app.py 去掉 Flask fallback;入口默认根路径模式(API_PREFIX 可选) ## 删除 requests 技术债 - 新增 core/sync_http.py:requests 风格的同步门面(底层 aiohttp 连接池, 流式读取经有界队列背压),含 Session/headers/(connect,read) 元组超时兼容 - core/utils、auth_system、podcast_api、music_downloader、services 全部切换 - 实测真实下载 5.2MB 音频完整成功 ## 质量门禁归零 - ruff:0 错误(B904 全部修复,不再忽略) - pyright:0 错误(类型收窄修复) - pytest:140/140(含 golden 契约回归、root 模式 66 项路径覆盖) - 真实 uvicorn 启动:/health、/、/static、受保护端鉴权全部正确 新增 tests/golden/responses.json:删除 Flask 前捕获的 32 例响应快照, FastAPI 逐字段复现——删除巨石后行为回归仍有硬证据。
80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""下载/播客任务登记(FastAPI 栈)——镜像 main._register_download_task。
|
|
|
|
与 Flask 版的唯一差异:不依赖 flask.g,请求级账本键由 Billing 显式携带。
|
|
其余(归属、容量闸、task_id 白名单与碰撞换新、账本承接与撞名重试)逐条对齐。
|
|
"""
|
|
import logging
|
|
import re
|
|
import uuid
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
from core.config import MAX_ACTIVE_TASKS_PER_USER, MAX_TERMINAL_TASKS, MAX_TOTAL_TASKS
|
|
from core.state import DOWNLOAD_TASKS, DOWNLOAD_TASKS_LOCK
|
|
from web.billing import Billing
|
|
|
|
logger = logging.getLogger('web.tasks')
|
|
|
|
_TASK_ID_RE = re.compile(r'^[A-Za-z0-9_-]{1,32}$')
|
|
|
|
|
|
async def register_download_task(billing: Billing, task: dict[str, Any],
|
|
task_id_hint: str = '') -> str | None:
|
|
"""任务登记:失败返回错误描述,成功返回 None。task['task_id'] 为最终 id。"""
|
|
user_id = billing.user.id
|
|
task['user_id'] = user_id
|
|
task['billing_reserved'] = billing.need
|
|
|
|
with DOWNLOAD_TASKS_LOCK:
|
|
tid = (task_id_hint or '').strip() or uuid.uuid4().hex[:12]
|
|
if not _TASK_ID_RE.match(tid):
|
|
tid = uuid.uuid4().hex[:12]
|
|
while tid in DOWNLOAD_TASKS:
|
|
tid = uuid.uuid4().hex[:12]
|
|
active = sum(1 for t in DOWNLOAD_TASKS.values()
|
|
if t.get('user_id') == user_id
|
|
and t.get('status') in ('queued', 'running', 'paused'))
|
|
if active >= MAX_ACTIVE_TASKS_PER_USER:
|
|
return f'您有 {active} 个任务正在进行,请等待完成或取消后再提交'
|
|
active_total = sum(1 for t in DOWNLOAD_TASKS.values()
|
|
if t.get('status') in ('queued', 'running', 'paused'))
|
|
if active_total >= MAX_TOTAL_TASKS:
|
|
return '当前任务队列已满,请稍后再试'
|
|
if len(DOWNLOAD_TASKS) - active_total >= MAX_TERMINAL_TASKS:
|
|
return '当前任务队列已满,请稍后再试'
|
|
task['task_id'] = tid
|
|
DOWNLOAD_TASKS[tid] = task
|
|
|
|
# 账本承接(撞名重试,与 Flask 同口径)
|
|
try:
|
|
renamed = await billing.rename_to(tid)
|
|
retries = 0
|
|
while not renamed and retries < 5:
|
|
retries += 1
|
|
with DOWNLOAD_TASKS_LOCK:
|
|
DOWNLOAD_TASKS.pop(tid, None)
|
|
tid = uuid.uuid4().hex[:12]
|
|
while tid in DOWNLOAD_TASKS:
|
|
tid = uuid.uuid4().hex[:12]
|
|
task['task_id'] = tid
|
|
DOWNLOAD_TASKS[tid] = task
|
|
renamed = await billing.rename_to(tid)
|
|
if not renamed:
|
|
raise RuntimeError('任务账本键撞名,请重新提交')
|
|
billing.ledger_key = tid
|
|
except Exception:
|
|
with DOWNLOAD_TASKS_LOCK:
|
|
if DOWNLOAD_TASKS.get(tid) is task:
|
|
DOWNLOAD_TASKS.pop(tid, None)
|
|
raise
|
|
return None
|
|
|
|
|
|
async def start_task(task_id: str, worker: Callable, args: tuple = ()) -> str | None:
|
|
"""启动 worker 线程(复用 services 的 _start_task_thread,失败收敛终态并退费)。"""
|
|
import asyncio
|
|
|
|
import services.runtime as services
|
|
return await asyncio.to_thread(services._start_task_thread, task_id, worker, args)
|