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
+55
View File
@@ -486,6 +486,8 @@ def init_db():
("start_label", "TEXT"), ("end_label", "TEXT"),
("returned_at", "TIMESTAMP"),
("rounds_override", "INTEGER"),
# 用户微信凭据失效时暂停自动还车;保存新凭据后清除,避免重启后无限重试。
("credential_blocked_at", "TIMESTAMP"),
]:
try:
cursor.execute(f"ALTER TABLE rentals ADD COLUMN {col_def[0]} {col_def[1]}")
@@ -705,6 +707,59 @@ def save_user_rental_settings(user_id: int, settings: dict):
return bool(changed)
def mark_user_credential_blocked(user_id: int) -> int:
"""把某用户所有进行中订单标记为「凭据失效,暂停自动还车」。
只写一次,重复调用不刷时间;返回本次新增阻断的订单数。
"""
if not user_id:
return 0
conn = get_db()
try:
cur = conn.execute("""
UPDATE rentals SET credential_blocked_at = datetime('now', '+8 hours')
WHERE user_id = ? AND expired = 0 AND credential_blocked_at IS NULL
""", (user_id,))
conn.commit()
return cur.rowcount
except sqlite3.OperationalError:
return 0 # 老库尚未迁移时不影响主流程
finally:
conn.close()
def is_rental_credential_blocked(rental_id: int) -> bool:
"""该单是否因用户凭据失效而暂停自动还车。"""
conn = get_db()
try:
row = conn.execute(
"SELECT credential_blocked_at FROM rentals WHERE id = ?", (rental_id,)
).fetchone()
return bool(row and row["credential_blocked_at"])
except sqlite3.OperationalError:
return False
finally:
conn.close()
def clear_user_credential_blocks(user_id: int) -> int:
"""保存新凭据后恢复该用户的自动还车,返回解除阻断的订单数。"""
if not user_id:
return 0
conn = get_db()
try:
cur = conn.execute("""
UPDATE rentals SET credential_blocked_at = NULL
WHERE user_id = ? AND credential_blocked_at IS NOT NULL
""", (user_id,))
conn.commit()
return cur.rowcount
except sqlite3.OperationalError:
return 0
finally:
conn.close()
# ===== 注册邀请码 =====
def create_invite_codes(count: int, max_uses: int, note: str = ""):