Archived
fix: 恢复任务监控线程的启动点(迁移中丢失,超时取消与账本对账静默失效)
为什么:d209af8 基线在 main.py:4594 用 threading.Thread(...).start() 启动 _task_monitor_loop;8446166「移除 Flask」把实现搬进 services/runtime.py 时 只搬了函数定义、丢了启动点,6288a98 拆包后落到 services/tasks.py,HEAD 仍 无任何调用方——函数体完好,但一次都没跑过。 后果(自该次迁移起持续失效): - 心跳超时不再暂停离线设备的批量任务,排队名额无法释放 - 离线 >30 分钟 / 运行 >4 小时的任务不再自动取消 - 终态任务记录、tasks/ 孤儿目录、downloads 缓存不再回收 - ledger_recover_orphans 停摆,预扣未退款的账永久悬挂在 open 状态 改法:新增 start_task_monitor(),加锁保证幂等(重复调用返回 False), 在 create_app 的 lifespan 里用 asyncio.to_thread 启动;新增 start_monitor 开关供测试显式关闭。裸 TestClient(app) 不进上下文管理器、不触发 lifespan, 故既有测试不受影响。 验证:pytest 新增 3 例(幂等 / lifespan 启动 / 可跳过);端到端 uvicorn 冒烟 日志出现「任务监控线程已启动」。
This commit is contained in:
1 parent
095171e0b7
commit
0b25f215a2
3 files changed
+137
-20
No files matched your search
+41
-20
@@ -35,26 +35,44 @@ from web.routers import tasks as tasks_router
|
||||
from web.routers import user as user_router
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def _lifespan(app: FastAPI):
|
||||
"""应用生命周期:启动确保站点库表结构就绪,关闭时释放 aiohttp 连接池。
|
||||
def _make_lifespan(start_monitor: bool):
|
||||
"""构造 lifespan(是否启动任务监控线程由 create_app 参数决定)。"""
|
||||
|
||||
启动必须建表。全新部署时宿主机的 data/ 往往是个空目录(compose 用
|
||||
`./data:/app/data` 挂载,会盖掉镜像内的任何内容),此时若只等首次查询
|
||||
让 sqlite3 自动建库,得到的是一张 **0 表空库**——随后所有涉库端点报
|
||||
`no such table: users`(实测登录端点直接 500,且错误被吞成"请稍后重试",
|
||||
极难定位)。init_db 全程 `CREATE TABLE IF NOT EXISTS` + 幂等迁移,
|
||||
对已有库重复执行无副作用,故放启动期最稳妥。
|
||||
"""
|
||||
await asyncio.to_thread(auth_system.init_db)
|
||||
yield
|
||||
try:
|
||||
from core import aiohttp_compat, http_client
|
||||
@asynccontextmanager
|
||||
async def _lifespan(app: FastAPI):
|
||||
"""应用生命周期:建表 → 启动任务监控 → 关闭时释放连接池与后台 loop。
|
||||
|
||||
await http_client.client.close()
|
||||
await aiohttp_compat.close()
|
||||
except Exception:
|
||||
pass
|
||||
启动必须建表。全新部署时宿主机的 data/ 往往是个空目录(compose 用
|
||||
`./data:/app/data` 挂载,会盖掉镜像内的任何内容),此时若只等首次查询
|
||||
让 sqlite3 自动建库,得到的是一张 **0 表空库**——随后所有涉库端点报
|
||||
`no such table: users`(实测登录端点直接 500,且错误被吞成"请稍后重试",
|
||||
极难定位)。init_db 全程 `CREATE TABLE IF NOT EXISTS` + 幂等迁移,
|
||||
对已有库重复执行无副作用,故放启动期最稳妥。
|
||||
|
||||
任务监控线程也必须在此启动:Flask 单体时代由模块级 `threading.Thread(
|
||||
target=_task_monitor_loop).start()` 拉起(基线 main.py:4594),移除 Flask
|
||||
迁到 FastAPI 时该启动点丢失,此后监控循环从未运行——心跳超时不再暂停
|
||||
离线设备、超时任务不再取消、过期任务与下载缓存不再回收、账本孤儿对账
|
||||
不再执行(预扣未退款的账永久悬挂)。详见 services.tasks.start_task_monitor。
|
||||
"""
|
||||
await asyncio.to_thread(auth_system.init_db)
|
||||
if start_monitor:
|
||||
from services.tasks import start_task_monitor
|
||||
|
||||
await asyncio.to_thread(start_task_monitor)
|
||||
yield
|
||||
try:
|
||||
from core import aiohttp_compat, async_bridge, http_client
|
||||
|
||||
await http_client.client.close()
|
||||
await aiohttp_compat.close()
|
||||
# 停掉 sync↔async 桥的后台事件循环(daemon 线程不调也能随进程退出,
|
||||
# 但显式关闭更干净)
|
||||
async_bridge.shutdown()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return _lifespan
|
||||
|
||||
|
||||
def _init_audio_secret() -> None:
|
||||
@@ -67,12 +85,15 @@ def _init_audio_secret() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def create_app(api_prefix: str = '') -> FastAPI:
|
||||
def create_app(api_prefix: str = '', *, start_monitor: bool = True) -> FastAPI:
|
||||
"""创建 FastAPI 应用。
|
||||
|
||||
api_prefix:路由挂载前缀。
|
||||
- ''(默认):直接服务前端使用的根路径(生产形态)
|
||||
- '/api/v1':挂在子前缀下(便于契约测试与反向代理)
|
||||
start_monitor:是否随应用启动任务监控线程。生产恒为 True;
|
||||
测试若以 `with TestClient(app)` 方式进入 lifespan,可传 False
|
||||
以避免后台线程清扫用例的临时目录。
|
||||
"""
|
||||
_init_audio_secret()
|
||||
|
||||
@@ -82,7 +103,7 @@ def create_app(api_prefix: str = '') -> FastAPI:
|
||||
docs_url='/api/v1/docs',
|
||||
redoc_url=None,
|
||||
openapi_url='/api/v1/openapi.json',
|
||||
lifespan=_lifespan,
|
||||
lifespan=_make_lifespan(start_monitor),
|
||||
)
|
||||
|
||||
# 响应侧统一处理:安全头 + 音频直链签名保护 + 版本指纹抹除
|
||||
|
||||
Reference in new issue
Block a user