From 1e223e847bd859ec69b3bfe609699ab165762eb9 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Tue, 15 Sep 2026 13:59:13 +0800 Subject: [PATCH] =?UTF-8?q?chore(=E9=80=9A=E7=9F=A5):=20=E8=A1=A5=E4=B8=8A?= =?UTF-8?q?=E6=BC=8F=E6=8F=90=E4=BA=A4=E7=9A=84=20web=5Fserver.py=20?= =?UTF-8?q?=E6=8E=A5=E7=BA=BF=EF=BC=88init=5Fapp=20=E6=97=B6=E5=BA=8F=20+?= =?UTF-8?q?=20=E5=90=AF=E5=8A=A8/=E5=81=9C=E6=AD=A2=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一轮的 git add 路径列表漏了 web_server.py——功能本身在跑(文件在磁盘上), 但提交里缺这一段,别人拉下来会因为没调 init_app 而收不到任何通知。 --- web_server.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/web_server.py b/web_server.py index 51ffd8c..31be35c 100644 --- a/web_server.py +++ b/web_server.py @@ -66,6 +66,13 @@ def _consume_pending_restore(): # 先初始化数据库(含旧 JSON 迁移),再创建 TaskManager(需要 app context 读写 DB) init_db(app) +# 通知模块:**必须早于 _consume_pending_restore()**——启动期就要能发「备份已恢复」 +# 这类通知(notify 在未就绪时静默丢弃,晚初始化就丢事件了) +try: + from core import notifier as _notifier + _notifier.init_app(app) +except Exception as _e: # 通知出问题不影响启动 + _log.warning(f"通知模块初始化失败(不影响启动): {_e}") # 消费「待生效的备份恢复」——必须在 init_db 之后(表已建好,且现在是事务替换而非换文件) _consume_pending_restore() # 库环境标签校验 + 启动横幅:每次启动都明确写出「现在连的是哪个库」, @@ -288,9 +295,27 @@ if __name__ == "__main__": # 预连接设备池:adb server 重启后设备全掉线,后台并发重连加速恢复 _preconnect_pool_devices() _log.info(f"管理后台: http://localhost:{WEB_PORT}/ (admin/admin123)") + # 启动通知:放在 __main__ 里是刻意的——以 WSGI 方式 import 本模块只有"半个启动" + # (阶段 A~F 在 import 期就跑完了),不该发"服务已启动"。 + try: + from core import notifier as _notifier + with app.app_context(): + _devs = len(device_pool.list_configured()) + _notifier.notify("service.started", env=db_config.env_badge().get("env", ""), + db_target=db_config.describe_target(), + device_count=_devs, job_count=len(mgr.jobs)) + except Exception as _e: + _log.warning(f"发送启动通知失败(不影响启动): {_e}") + _t0 = time.time() try: _run_server(WEB_HOST, WEB_PORT) finally: + try: + from core import notifier as _notifier + _notifier.notify("service.stopping", uptime_s=int(time.time() - _t0)) + _notifier.shutdown(timeout=3.0) + except Exception: + pass mgr.shutdown() device_discovery.shutdown() _stop_uiauto()