fix(P0): 后台线程改为幂等 bootstrap(),僵尸订单由巡检定期兜底
1) 后台线程绑定在 __main__ 里 → WSGI/`flask run` 启动时全部静默不启动 - 新增 app.bootstrap():init_db + 恢复计时器 + 补还超期单 + 订单巡检 + 数据采集 + 订阅调度 + 告警调度,threading.Lock 保证只执行一次 - @app.before_request 首次请求兜底调用(WSGI 部署也能起来), __main__ 同样调 bootstrap() 2) expired=0 且 end_time 已过的僵尸单只在启动时清一次 - 新增 timer.sweep_overdue_rentals():全局扫描超期未还的单,按 _should_auto_return 过滤后排队 _run_recovery,进程内 10 分钟节流 - 巡检线程循环每轮调用一次(放在 _user_order_checker_loop,不污染单测) - reminder 来源的单没有上游订单,就地关闭(expired=1 + returned_at), 否则用户页面横幅永远显示「用车中」 3) 顺手修的 bug - sweep 里先把 sqlite3.Row 转 dict 再关连接,避免关库后读行 - _close_local_reminder 兼容没有 returned_at 列的极简 schema - 该测试自带的连接泄漏(断言抛错时没关库)导致 teardown PermissionError 实测(真实库):启动时 3 单真订单排队补还车,44 条本地提醒单就地关闭 测试:157 pytest(新增 sweep 排队/节流/本地单关闭)+ 32 node 全绿
This commit is contained in:
@@ -654,29 +654,76 @@ def api_history_heatmap():
|
||||
|
||||
|
||||
# ========== 启动 ==========
|
||||
# 后台线程原来只挂在 `if __name__ == "__main__":` 里,换成 gunicorn/`flask run`
|
||||
# 或任何 WSGI 启动方式时全都不会启动(且静默)——定时采集、订单巡检、
|
||||
# 自动还车、订阅提醒、凭据告警会一起残废。这里收进幂等的 bootstrap(),
|
||||
# WSGI 与 __main__ 都会调用,再由 before_request 兜住「首次请求」。
|
||||
_bootstrap_lock = threading.Lock()
|
||||
_bootstrap_done = False
|
||||
|
||||
|
||||
def bootstrap():
|
||||
"""初始化数据库并启动所有后台线程。幂等:重复调用只会执行一次。"""
|
||||
global _bootstrap_done
|
||||
with _bootstrap_lock:
|
||||
if _bootstrap_done:
|
||||
return False
|
||||
_bootstrap_done = True
|
||||
|
||||
print("[启动] bootstrap: 初始化数据库 + 启动后台线程")
|
||||
try:
|
||||
init_db()
|
||||
except Exception as exc:
|
||||
print(f"[启动] init_db 失败: {exc}")
|
||||
|
||||
steps = [
|
||||
("恢复计时器", restore_active_timers),
|
||||
("补还超期订单", _sweep_on_boot),
|
||||
("订单巡检", start_all_order_checkers),
|
||||
("数据采集", start_snapshot_scheduler),
|
||||
]
|
||||
for name, fn in steps:
|
||||
try:
|
||||
fn()
|
||||
except Exception as exc:
|
||||
print(f"[启动] {name} 失败: {exc}")
|
||||
|
||||
try:
|
||||
from subscribe import start_subscribe_scheduler
|
||||
start_subscribe_scheduler()
|
||||
except Exception as exc:
|
||||
print(f"[启动] 订阅调度失败: {exc}")
|
||||
|
||||
try:
|
||||
from alerts import start_alert_scheduler
|
||||
start_alert_scheduler()
|
||||
except Exception as exc:
|
||||
print(f"[启动] 告警调度失败: {exc}")
|
||||
|
||||
print("[启动] bootstrap 完成")
|
||||
return True
|
||||
|
||||
|
||||
def _sweep_on_boot():
|
||||
"""启动时把「expired=0 且已过期」的僵尸单捞一遍(带 5 分钟节流)。"""
|
||||
try:
|
||||
from timer import sweep_overdue_rentals
|
||||
n = sweep_overdue_rentals(force=True)
|
||||
if n:
|
||||
print(f"[启动] 补还超期订单 {n} 单")
|
||||
except Exception as exc:
|
||||
print(f"[启动] 补还超期订单失败: {exc}")
|
||||
|
||||
|
||||
@app.before_request
|
||||
def _ensure_bootstrap():
|
||||
"""WSGI 部署(gunicorn 等)不会走 __main__,靠首个请求把后台线程拉起来。"""
|
||||
if not _bootstrap_done:
|
||||
bootstrap()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from pathlib import Path
|
||||
|
||||
# 初始化数据库
|
||||
init_db()
|
||||
|
||||
# 恢复未过期的计时器
|
||||
restore_active_timers()
|
||||
|
||||
# 启动用户订单检测
|
||||
start_all_order_checkers()
|
||||
|
||||
# 启动数据采集
|
||||
start_snapshot_scheduler()
|
||||
|
||||
# 启动订阅提醒调度(定时查看教学楼附近车辆 → 邮件)
|
||||
from subscribe import start_subscribe_scheduler
|
||||
start_subscribe_scheduler()
|
||||
|
||||
# 启动凭据到期告警调度(1天/1小时/已过期 三级)
|
||||
from alerts import start_alert_scheduler
|
||||
start_alert_scheduler()
|
||||
|
||||
bootstrap()
|
||||
PORT = 98
|
||||
# 只监听本机回环:对外统一走 nginx 的 HTTPS(同机回环转发到本端口)。
|
||||
# 绑定 0.0.0.0 会让明文 HTTP 绕过 nginx 直接暴露,token/凭据可被局域网窃听。
|
||||
|
||||
Reference in New Issue
Block a user