feat(gateway): 账号只读查询通道——从已登录账号取真实订单
上游要的不只是网关记的任务状态镜像,还有「已登录账号在站点上的真实订单」,
但账号只在 NAT 后本地机上,只能经网关队列走。新增独立查询通道(§11):
- gateway 单开 account_queries 表 + QueryStatus 状态机,接口
POST /api/account/queries(幂等)/ lease / {id}/result / {id}
- 不复用下单任务队列:查询是只读,租约过期可安全重投(与下单「绝不自动
重投」相反),且不该被全局并发度 1 堵死、task_reports 是订单镜像不能污染
- 本地交易服务起第二条常驻循环 query_runner,领到即调 SiteInteractor 真读:
order_list 复用已实测的 list_recent_orders(规范化字段 + 站点
orderListData 原文),order_detail 复用 fetch_order_detail(配送阶段 +
页面 __INITIAL_STATE__ 原样透传,结构未经真实样本,不抽字段)
- 账号级串行仍由 SiteInteractor 的锁保证;每次执行套超时按失败回报
- 错误码 6005/6006(查询通道,可重试只读区别于 6001-6004);/health 暴露
queued_query_count;结果体积上限先丢原始 JSON
openapi.json 重导,docs/order-gateway.md §11、README、.env.example 补全
配置与实测边界。全量测试 404→454 通过。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+105
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
intent 字段刻意保留成 `dict[str, Any]`——网关不解释下单意图,结构由 trading 侧
|
||||
定义。网关只负责把它存下来、原样吐给 worker,避免业务规则悄悄渗进任务队列。
|
||||
账号只读查询的 `params` / `result` 同理。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -9,7 +10,7 @@ from typing import Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.shared.task_state import OrderState, TaskStatus
|
||||
from app.shared.task_state import AccountQueryKind, OrderState, QueryStatus, TaskStatus
|
||||
|
||||
|
||||
# ---- POST /api/orders ----
|
||||
@@ -159,6 +160,106 @@ class TaskListData(BaseModel):
|
||||
offset: int
|
||||
|
||||
|
||||
# ---- 账号只读查询通道(见 docs/order-gateway.md §11)----
|
||||
|
||||
|
||||
class SubmitQueryRequest(BaseModel):
|
||||
"""上游提交一张账号只读查询单
|
||||
|
||||
query_id 语义同 task_id:上游自带的幂等键,重复提交返回既有单。
|
||||
params 由网关原样透传给 worker,结构由 trading 侧定义(见 §11.2):
|
||||
- order_list:`{"since": ISO8601?, "max_pages": int?}`
|
||||
- order_detail:`{"order_number": "306087-20260813-0863947697"}`
|
||||
"""
|
||||
|
||||
query_id: str | None = None
|
||||
site: str = "rakuten"
|
||||
kind: AccountQueryKind
|
||||
params: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class SubmitQueryData(BaseModel):
|
||||
"""提交响应。纯异步:这里只拿到单号,结果去 GET /api/account/queries/{id} 取"""
|
||||
|
||||
query_id: str
|
||||
status: QueryStatus
|
||||
created: bool # True=本次新建,False=命中既有单(幂等)
|
||||
|
||||
|
||||
class QueryLeaseData(BaseModel):
|
||||
"""GET /api/account/queries/lease 的响应
|
||||
|
||||
无可领查询单时整个 data 为 null(HTTP 仍 200)。没有 known_state 之类的字段——
|
||||
查询是无状态的一次性动作,attempt > 1 只是说明上一轮超时被重投了,worker
|
||||
不需要为此改变行为(只读,重跑安全)。
|
||||
"""
|
||||
|
||||
query_id: str
|
||||
site: str
|
||||
kind: AccountQueryKind
|
||||
params: dict[str, Any]
|
||||
lease_expires_at: str # ISO8601 UTC
|
||||
attempt: int
|
||||
|
||||
|
||||
class QueryResultRequest(BaseModel):
|
||||
"""worker 回报查询结果
|
||||
|
||||
success=true 时 result 必填;false 时 error_message 必填、error_code 可选
|
||||
(沿用 shared.errors 的错误码,便于上游按同一张表分支)。
|
||||
"""
|
||||
|
||||
worker_id: str
|
||||
success: bool
|
||||
result: dict[str, Any] | None = None
|
||||
error_code: int | None = None
|
||||
error_message: str = ""
|
||||
|
||||
|
||||
class QueryResultData(BaseModel):
|
||||
"""回报响应"""
|
||||
|
||||
query_id: str
|
||||
status: QueryStatus
|
||||
|
||||
|
||||
class QueryError(BaseModel):
|
||||
"""查询失败的原因"""
|
||||
|
||||
code: int | None = None
|
||||
message: str = ""
|
||||
|
||||
|
||||
class QueryDetail(BaseModel):
|
||||
"""单张查询单的完整视图
|
||||
|
||||
result 是 worker 回的原文(站点原始 JSON + 规范化字段),网关不解释内容。
|
||||
"""
|
||||
|
||||
query_id: str
|
||||
site: str
|
||||
kind: AccountQueryKind
|
||||
params: dict[str, Any]
|
||||
status: QueryStatus
|
||||
lease_owner: str | None = None
|
||||
lease_expires_at: str | None = None
|
||||
attempts: int = 0
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None = None
|
||||
result: dict[str, Any] | None = None
|
||||
error: QueryError | None = None
|
||||
|
||||
|
||||
class QueryListData(BaseModel):
|
||||
"""查询单列表(运维排查用)"""
|
||||
|
||||
items: list[QueryDetail]
|
||||
total: int
|
||||
limit: int
|
||||
offset: int
|
||||
|
||||
|
||||
# ---- GET /health ----
|
||||
|
||||
|
||||
@@ -188,6 +289,9 @@ class GatewayHealthData(BaseModel):
|
||||
|
||||
status: str # "ok" 或 "degraded"
|
||||
queued_count: int
|
||||
# 等待本地 worker 领取的账号只读查询单数量。查询没有「无人领即告警」这条
|
||||
# 规则(它有自己的 TTL 会自动 expired),这里只是给运维一个可见的积压信号。
|
||||
queued_query_count: int = 0
|
||||
active_tasks: list[TaskDetail] = Field(default_factory=list)
|
||||
workers: list[WorkerHealthEntry] = Field(default_factory=list)
|
||||
offline_workers: list[WorkerHealthEntry] = Field(default_factory=list)
|
||||
|
||||
Reference in New Issue
Block a user