fix: 凭据失效即暂停自动还车,更新凭据后恢复

问题
- 上游 102001/token_invalid 被当成普通可重试错误,内部三次重试与巡检退避反复执行。
- 6 小时上限存在内存中,滚动部署后清零,僵尸订单每轮重新刷失败日志。

修复
- rentals 新增 credential_blocked_at(不新增表,幂等迁移)。
- /project/bicycle 或 /record/return 明确返回凭据失效时立即停,不再做第 2/3 次重试;
  同一用户所有进行中订单一并阻断,避免其它订单继续刷。
- 自动还车入口、Timer 重试、巡检退避与 recovery 节流均识别阻断;重启后也不打上游。
- 保存用户凭据(个人中心或管理端)时清除阻断并重启订单检测,自动恢复收尾。
- 普通关锁失败仍保留原三次重试,不误伤临时故障。

测试
- 新增 5 例:失效只打一次、同用户多单阻断、普通故障仍重试、重启后不打上游、
  保存新凭据后解除并重启巡检、车辆查询失效不再调用还车。
- 全量 271 pytest + 45 node 全绿。
This commit is contained in:
zzy
2026-11-19 15:52:41 +08:00
parent 3b9363e37e
commit 364b1c77c6
5 changed files with 242 additions and 1 deletions
+40 -1
View File
@@ -6,7 +6,8 @@ import time as _time
import datetime as dt
import sqlite3
from db import get_db, get_user_auth, get_user_rental_settings
from db import (get_db, get_user_auth, get_user_rental_settings,
mark_user_credential_blocked, is_rental_credential_blocked)
from wechat import request_api, request_with_user_auth
from rental_mail import mark_return_tokens_used, send_rental_notification
@@ -557,6 +558,29 @@ def _align_to_park_area(user_id: int, latitude, longitude):
return latitude, longitude, False, 0.0
def _is_credential_failure(result) -> bool:
"""上游已明确拒绝用户凭据:继续重试只会在每次部署后制造新的无效请求。"""
if not isinstance(result, dict):
return False
if result.get("token_invalid") or result.get("rtn_code") == 102001:
return True
message = str(result.get("message") or "")
return result.get("rtn_code") == 401 and "Token ID" in message
def _pause_for_invalid_credential(rental_id: int, user_id: int, result) -> dict:
"""落库暂停该用户的自动还车,等待保存新凭据后恢复。"""
newly_blocked = mark_user_credential_blocked(user_id)
message = (result or {}).get("message") or "微信凭据需要更新"
print(f"[自动还车] rental_id={rental_id} 用户凭据失效,"
f"新增暂停 {newly_blocked} 单;保存新凭据后自动恢复")
return {
"success": False, "skipped": True, "credential_blocked": True,
"attempted": True, "rtn_code": (result or {}).get("rtn_code", 102001),
"message": message,
}
def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = False):
"""自动还车(含重试:最多3次),返回明确的成败结果。
@@ -580,10 +604,15 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = False)
return {"success": False, "rtn_code": 404, "message": "本地租车记录不存在"}
if rental["expired"]:
return {"success": True, "rtn_code": 0, "message": "订单已结束"}
if is_rental_credential_blocked(rental_id):
return {"success": False, "skipped": True, "credential_blocked": True,
"message": "微信凭据已失效,自动还车暂停;保存新凭据后自动恢复"}
bike_code = rental["bike_code"]
# 统一使用用户 token 查询车辆(与开锁/下单保持同一身份)
bike_result = request_with_user_auth(user_id, "POST", "/project/bicycle", data={"code": bike_code})
if _is_credential_failure(bike_result):
return _pause_for_invalid_credential(rental_id, user_id, bike_result)
latitude = bike_result.get("data", {}).get("latitude", 36.67) if bike_result.get("data") else 36.67
longitude = bike_result.get("data", {}).get("longitude", 116.98) if bike_result.get("data") else 116.98
@@ -609,6 +638,8 @@ def _auto_return_bike(rental_id: int, user_id: int, allow_renewal: bool = False)
data={"order_id": upstream_order_id, "longitude": longitude,
"latitude": latitude, "code": bike_code}
)
if _is_credential_failure(return_result):
return _pause_for_invalid_credential(rental_id, user_id, return_result)
if return_result.get("rtn_code") == 0:
print(f"[自动还车] 第{attempt}次调用成功")
break
@@ -774,6 +805,8 @@ def _recover_throttled(rental_id: int) -> bool:
已耗时决定(30 分钟内恒 60s,之后 60/120/300s 封顶),此时以退避表为准,
不再叠加基础间隔,否则 60 秒的退避会被 5 分钟基础间隔吃掉。
"""
if is_rental_credential_blocked(rental_id):
return True
now = _time.time()
due = _auto_return_backoff_until.get(rental_id)
if due is not None:
@@ -1094,6 +1127,9 @@ def _advance_auto_return_backoff(rental_id: int, message: str = "") -> int:
只有在快保次数已经用完(n >= AUTO_RETURN_MAX_RETRIES)时才生效 ——
否则快保阶段由 Timer 链自己推进,两套机制同时改计数会互相抢节奏。
"""
if is_rental_credential_blocked(rental_id):
_clear_auto_return_state(rental_id)
return 0
n = _auto_return_retries.get(rental_id, 0)
if n < len(AUTO_RETURN_RETRY_DELAYS):
return 0
@@ -1194,6 +1230,9 @@ def _schedule_auto_return_retry(rental_id: int, email: str, bike_code: str, user
`notified_0` 保持 0,让各处调度(巡检 / 兜底扫描 / 页面横幅重试)还能再次触发。
"""
if is_rental_credential_blocked(rental_id):
_clear_auto_return_state(rental_id)
return
now_ts = _time.time()
first_fail = _auto_return_first_fail_at.setdefault(rental_id, now_ts)
if now_ts - first_fail > AUTO_RETURN_GIVE_UP_SECONDS: