refactor: 删除 services.runtime 兼容门面,7 处调用点直连真实子模块

为什么:上一次分包重构把 1900 行巨石按职责拆成 9 个子模块后,留了一个
runtime.py 作为「原样再导出」的兼容门面,好让调用点零改动。但迁移期结束后
没有一个调用点迁走——门面成了永久中间层:每次读代码都要先跳一层才知道
符号真正住在哪,pyright 也无法从门面追溯定义位置。

改法:保持「函数内延迟导入」的形式不变,只把导入目标换成真实子模块。
**刻意不提到模块顶部**——services/__init__.py 里有依赖自检
(缺 auth_system/podcast_api/music_api/music_downloader/qr_login 就 sys.exit(1)),
提到顶层会让「import web.*」这一步就触发该自检,改变启动语义,超出本次范围。

7 处调用点(AST 精确扫描确认全集):
- web/routers/download.py ×4:download / download_batch_zip / podcast_download /
  podcast_batch_zip,分别直连 services.downloads / batch / podcast / tasks
  外加 _shared._build_download_base 与 api_service.api_service
- web/routers/tasks.py ×2:_semaphores 取 _shared.BATCH_ZIP_SEM + api_service,
  topen 取 _shared.ENDPOINT_NAMES
- web/tasks_registry.py ×1:start_task 直连 _taskcore._start_task_thread

子模块短名统一加 `services_` 前缀别名(services_tasks 等),避免与函数内
既有局部名相撞。

顺带清理:9 个子模块 docstring 里「(自 services/runtime.py 拆出……)」的
历史说明在门面删除后已成悬空引用,一并去掉;包的模块划分说明改为
「不再有兼容门面层」。

验证:
- 符号可达:11 个被引符号在真实子模块上全部 hasattr 通过;
  services.runtime 已 ModuleNotFoundError;api_service 仍是同一实例对象
- 三个调用方模块均可正常 import
- 门禁:ruff 0 · pyright 0 errors 0 warnings · pytest 175 passed
- 真实 uvicorn 冒烟 8/8(含 POST /auth/login 400),临时数据目录独立落库
  9 张表,生产 data/ 指纹三项一致
This commit is contained in:
snowy-yang committed 2026-09-26 14:48:05 +08:00
1 parent 3ed2f36b18
commit 7a70eef636
13 files changed
+29 -158

No files matched your search

+2 -2
View File
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
"""服务层:从原 Flask 巨石中剥离的框架无关实现(不含任何 Flask 依赖)。
模块划分(原先集中在 runtime.py 的 1900 行,现按职责拆分;`services.runtime`
保留为兼容门面,原样再导出下列全部符号,既有调用点零改动):
模块划分(原先集中在单个 1900 行模块,现按职责拆分;调用方直接
``from services import <子模块>``,不再有兼容门面层):
- ``api_service``:MusicAPIService 与进程级实例(config / api_service)
- ``_shared``:限流桶、请求统计、命名与共享常量
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""服务层共享状态与低层工具(限流桶、请求统计、命名与常量)。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import collections
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""任务生命周期原语(启动 / 终态 / 文件收尾 / 结算)。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import threading
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""服务类与进程级服务实例。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import logging
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""歌单/专辑批量打包 worker。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import os
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""单曲下载、账号探测与任务交付。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import os
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""播客单集与整台打包 worker。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import os
-120
View File
@@ -1,120 +0,0 @@
# -*- coding: utf-8 -*-
"""网易云音乐API服务主程序
提供网易云音乐相关API服务,包括:
- 歌曲信息获取
- 音乐搜索
- 歌单和专辑详情
- 音乐下载
- 健康检查
"""
# 兼容门面:实现已按职责拆分到同包各子模块,此处原样再导出。
# 用 `__all__` 声明再导出(ruff F401 与 pyright 均据此认定「有意再导出」),
# 使既有调用点(services.xxx / from services import Y)零改动。
from ._shared import (
_COUNTED_LOWER,
_LEDGER_LAST_CLEANUP,
_LEDGER_LAST_RECOVER,
_MONITOR_ERR_TS,
_QUIET_LOG_PATHS,
_TASKS_SWEEP_LAST,
BATCH_ZIP_SEM,
COUNTED_PATHS,
ENDPOINT_NAMES,
_build_download_base,
_rate_ok,
_record_log,
_sweep_rate_buckets,
)
from ._taskcore import (
_mark_task_terminal,
_settle_batch_billing,
_start_task_thread,
_task_files,
)
from .api_service import (
APIConfig,
APIResponse,
MusicAPIService,
api_service,
config,
)
from .batch import (
_batch_zip_worker,
)
from .downloads import (
_admin_client,
_detect_vip,
_global_music_u,
_parse_cookie_str,
_single_download_worker,
_task_delivery_copy,
_user_client,
_vip_name,
)
from .podcast import (
_podcast_cleanup_files,
_podcast_download_worker,
_podcast_resolve_audio,
_podcast_safe_name,
_podcast_stream_download,
_podcast_zip_worker,
)
from .streaming import (
_stream_open,
_SyncStreamResponse,
)
from .tasks import (
_quiesce_tasks_for_restore,
_sweep_download_cache,
_task_monitor_loop,
_touch_device,
)
__all__ = [
'BATCH_ZIP_SEM',
'COUNTED_PATHS',
'ENDPOINT_NAMES',
'_COUNTED_LOWER',
'_LEDGER_LAST_CLEANUP',
'_LEDGER_LAST_RECOVER',
'_MONITOR_ERR_TS',
'_QUIET_LOG_PATHS',
'_TASKS_SWEEP_LAST',
'APIConfig',
'APIResponse',
'MusicAPIService',
'_SyncStreamResponse',
'_admin_client',
'_batch_zip_worker',
'_build_download_base',
'_detect_vip',
'_global_music_u',
'_mark_task_terminal',
'_parse_cookie_str',
'_podcast_cleanup_files',
'_podcast_download_worker',
'_podcast_resolve_audio',
'_podcast_safe_name',
'_podcast_stream_download',
'_podcast_zip_worker',
'_quiesce_tasks_for_restore',
'_rate_ok',
'_record_log',
'_settle_batch_billing',
'_single_download_worker',
'_start_task_thread',
'_stream_open',
'_sweep_download_cache',
'_sweep_rate_buckets',
'_task_delivery_copy',
'_task_files',
'_task_monitor_loop',
'_touch_device',
'_user_client',
'_vip_name',
'api_service',
'config',
]
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""同步流式响应垫片(音频分段透传)。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
-2
View File
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""任务监控循环与设备心跳。
(自 services/runtime.py 拆出,行为与原实现一致。)
"""
import os