Archived
迁移端点:
- POST /api/v1/download(单曲:file 后台任务 / json 同步交付+结算)
- POST /api/v1/download/batch_zip(按去重歌曲数计费)
- POST /api/v1/podcast/download、/podcast/download/batch_zip
- 新增 web/billing.py(预扣+开账同事务 / 只退 open 行 / rename 承接)
与 web/tasks_registry.py(任务登记/容量闸/账本撞名重试,与 main 同口径)
- worker 仍复用 main 既有实现(无 flask 依赖),账本与交付语义不变
修复我引入的真实缺陷:
- download 参数校验分支(ID/音质/格式非法)漏退款 → 预扣泄漏。改为统一 _fail() 退款
- web/routers/{auth,register}.py 在导入期绑定 deps.secret_key,导致 patch web.deps.secret_key
无效、且测试顺序相关。改为调用期 deps.secret_key() 解析
测试:tests/test_download_billing.py 7 项沙箱用例如实断言账本守恒
(预扣必退、无 open 行残留、次数不足不建任务);
全套 pytest 69/69(顺序无关);ruff All checks passed;pyright 0 errors;奇偶 36/36。
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 线程(复用 main._start_task_thread 的实现,失败收敛终态并退费)。"""
|
|
import asyncio
|
|
|
|
import main as legacy
|
|
return await asyncio.to_thread(legacy._start_task_thread, task_id, worker, args)
|