polish: 还车回填返回行数 + 管理端兜底 + 节流记录成功即清理
1) _record_rental_end 返回命中行数 - _set_rental_finished 返回 cursor.rowcount,_record_rental_end 透传; 管理端代还车能知道「order_id 没匹配到任何一行」(脏 order_id 场景) 2) 管理端代还车兜底 - 回填命中 0 行时按 rental_id 补一次 UPDATE(expired=1 + returned_at), 后台立刻正确,不再等下一轮巡检自愈 3) _recover_last_at 成功后清理 - 新增 _run_recovery(rental_id, user_id):成功清掉节流记录,失败保留; 巡检与重启补还车都改走它 测试:154 pytest(新增脏 order_id 兜底、节流清理 2 条)+ 32 node 全绿
This commit is contained in:
@@ -585,6 +585,18 @@ def _recover_throttled(rental_id: int) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _run_recovery(rental_id: int, user_id: int):
|
||||
"""巡检/重启的补还车入口:成功后清掉节流记录,别让进程里留失败痕迹。"""
|
||||
try:
|
||||
result = auto_return_bike(rental_id, user_id) or {}
|
||||
if result.get("success"):
|
||||
_recover_last_at.pop(rental_id, None)
|
||||
return result
|
||||
except Exception as exc:
|
||||
print(f"[自动还车] 补还车异常 rental_id={rental_id}: {exc}")
|
||||
return {"success": False, "message": str(exc)}
|
||||
|
||||
|
||||
def _should_auto_return(rental) -> bool:
|
||||
"""这张单能不能走上游自动还车(restore / 巡检共用同一判断)。"""
|
||||
if not rental:
|
||||
@@ -904,7 +916,8 @@ def _recover_overdue_rental(rental: dict):
|
||||
if not user_id:
|
||||
print(f"[计时系统] rental_id={rental_id} 没有 user_id,跳过补还车")
|
||||
return
|
||||
result = auto_return_bike(rental_id, user_id)
|
||||
result = _run_recovery(rental_id, user_id) if _should_auto_return(rental) \
|
||||
else auto_return_bike(rental_id, user_id)
|
||||
print(f"[计时系统] 补还车结果 rental_id={rental_id}: {result.get('message')}")
|
||||
except Exception as exc:
|
||||
print(f"[计时系统] 补还车异常 rental_id={rental_id}: {exc}")
|
||||
@@ -1021,7 +1034,7 @@ def _check_user_order(user_id: int):
|
||||
"recovering": True}
|
||||
print(f"[订单检测] rental_id={rental['id']} 已过到期时间,补一次自动还车")
|
||||
threading.Thread(
|
||||
target=lambda: auto_return_bike(rental["id"], user_id),
|
||||
target=lambda: _run_recovery(rental["id"], user_id),
|
||||
daemon=True
|
||||
).start()
|
||||
return {"ok": True, "active": True, "order_id": order_id,
|
||||
|
||||
Reference in New Issue
Block a user