自动登录接口 + 有状态端容器化部署 + 三服务合并 openapi 导出

自动登录(此前只能人工跑 scripts/login.py 再 /api/auth/reload):
- 新增 POST /api/auth/login:动作顺序与下单前的 require_logged_in 一致(探测 →
  未登录则按 account.yaml 登一次 → 再探测),已登录直接跳过不白起浏览器。
  刻意**不抛 5001**:失败以 logged_in=false + 各站 detail 正常返回,调用方自己
  决定是人工接管还是换账号。
- 新增 RAKUTEN_AUTO_LOGIN_ON_START(默认 false):启动即准备登录态,为容器部署
  而存在(镜像里没有落盘的 storage_state)。做成后台任务而非启动阻塞——登录最长
  等 relogin_timeout_seconds(默认 300s,撞验证码时在等人工),阻塞会让 /health
  在这段时间里连端口都不通;关服务时 cancel 掉在途的那次。
- 自动登录不绕过站点校验:凭据是用户自己配在 account.yaml 里的,代填进站点自己的
  登录表单,撞 reCAPTCHA / 设备验证会停在有头浏览器等人工,等不到就超时失败。

容器化部署(新增 Dockerfile.trading + docker-compose.yml):
- 有状态端单独出镜像不是为了整洁:下单/结算必须用**有头** Chromium(headless 会让
  结算 SPA 失灵),镜像要带 Xvfb + 日文字体 + 给人工接管用的可选 x11vnc,抓取镜像
  没有这些。网关复用同一镜像只换 command。
- Jenkinsfile 一条流水线产出两个镜像,BUILD_SCRAPING / BUILD_TRADING 两个开关控制。
- .dockerignore 补上 account.yaml / .auth/ / .browser-data/ / data/:明文密码+卡号、
  可直接冒充账号的 cookie、带登录态的浏览器 profile、含真实 PII 的证据快照,都不该
  进镜像也不该进 build context,运行时一律走挂载。
- .env.example 里 RAKUTEN_AUTO_LOGIN_ON_START 刻意留成注释:compose 的变量插值与
  env_file 读的是同一个 ./.env,这里写成显式值会让 compose 的 `${...:-true}` 失效,
  按 compose 文件头「cp .env.example .env」走反而不会自动登录。

openapi 导出(scripts/export_openapi.py):三服务合并成一份可直接导入 Apifox /
Postman 的文档,每条接口带 operation 级 servers(不必手动切端口)。鉴权标注是遍历
FastAPI 依赖树认出真的挂了 require_bearer_token 的接口,不按路径猜。

openapi.json 本身仍是 gitignore 的本地生成物,因此 tests/test_openapi_export.py
只在内存里校验合并逻辑(三服务覆盖、operation 级 servers、除 /health 外全部标鉴权、
operationId 唯一、$ref 可解析),不断言「文件内容 == 当前导出结果」——CI 的全新
clone 里没有这个文件,那种断言必然失败。代价是「改了接口忘了重新导出」没有自动
兜底,得手动跑 --check,已在 README 里点明。

398 测试全绿;另单独验证过缺 openapi.json 时该文件 5 个用例仍通过(CI 场景)。
compose 的变量插值行为只按文档核对,本机没有 docker 未能实测。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-14 14:27:38 +08:00
co-authored by Claude Opus 5
parent e2875f0c00
commit 63c41b61e7
13 changed files with 1092 additions and 19 deletions
+43
View File
@@ -80,6 +80,34 @@ def build_container() -> TradingContainer:
return container
async def auto_login_on_start(container: TradingContainer) -> None:
"""启动后自动把登录态准备好(RAKUTEN_AUTO_LOGIN_ON_START=true 时)
为容器部署而存在:镜像里没有落盘的 storage_state,不希望每次新起容器都得先
在宿主机跑一遍 scripts/login.py。逐站点探测,未登录就按 account.yaml 登一次。
刻意做成后台任务而不是启动阻塞:登录最长要等 relogin_timeout_seconds(默认
300s,撞验证码时在等人工),阻塞会让 /health 在这段时间里连端口都不通。
任何失败都只记日志——服务照常提供 /health 与 /api/auth/*,人工接管后调
/api/auth/login 重试即可。
"""
for site in container.auth_session.sites:
try:
status = await container.auth_session.check(site)
if status.logged_in:
logger.info("启动自动登录跳过:site=%s 已是登录态", site)
continue
logger.info("启动自动登录:site=%s 当前未登录(%s", site, status.detail)
if await container.auth_session.try_relogin(site):
status = await container.auth_session.check(site)
logger.info(
"启动自动登录结束:site=%s logged_in=%s detail=%s",
site, status.logged_in, status.detail,
)
except Exception:
logger.exception("启动自动登录异常:site=%s(服务继续运行)", site)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理:登录态会话 + 站点交互器(必起)+ 可选 worker"""
@@ -101,6 +129,14 @@ async def lifespan(app: FastAPI):
assert container.site is not None
await container.site.start() # type: ignore[union-attr]
login_task: asyncio.Task | None = None
if container.settings.auto_login_on_start:
login_task = asyncio.create_task(
auto_login_on_start(container), name="trading-auto-login"
)
else:
logger.info("未开启 RAKUTEN_AUTO_LOGIN_ON_START,启动时不自动登录")
worker_task: asyncio.Task | None = None
if container.worker_runner is not None:
# 顺序:本地 DB 在 worker 写证据前先就绪;SiteInteractor 已在外层启动
@@ -123,6 +159,13 @@ async def lifespan(app: FastAPI):
try:
yield
finally:
if login_task is not None and not login_task.done():
# 关服务时正在登的那次不要了:浏览器由 login_one 自己的 async with 收尾
login_task.cancel()
try:
await login_task
except asyncio.CancelledError:
pass
if worker_task is not None:
container.worker_runner.stop() # type: ignore[union-attr]
worker_task.cancel()