chore(通知): 补上漏提交的 web_server.py 接线(init_app 时序 + 启动/停止通知)

上一轮的 git add 路径列表漏了 web_server.py——功能本身在跑(文件在磁盘上),
但提交里缺这一段,别人拉下来会因为没调 init_app 而收不到任何通知。
This commit is contained in:
2026-09-15 13:59:13 +08:00
parent 9a6c0db65c
commit 1e223e847b
+25
View File
@@ -66,6 +66,13 @@ def _consume_pending_restore():
# 先初始化数据库(含旧 JSON 迁移),再创建 TaskManager(需要 app context 读写 DB) # 先初始化数据库(含旧 JSON 迁移),再创建 TaskManager(需要 app context 读写 DB)
init_db(app) 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 之后(表已建好,且现在是事务替换而非换文件) # 消费「待生效的备份恢复」——必须在 init_db 之后(表已建好,且现在是事务替换而非换文件)
_consume_pending_restore() _consume_pending_restore()
# 库环境标签校验 + 启动横幅:每次启动都明确写出「现在连的是哪个库」, # 库环境标签校验 + 启动横幅:每次启动都明确写出「现在连的是哪个库」,
@@ -288,9 +295,27 @@ if __name__ == "__main__":
# 预连接设备池:adb server 重启后设备全掉线,后台并发重连加速恢复 # 预连接设备池:adb server 重启后设备全掉线,后台并发重连加速恢复
_preconnect_pool_devices() _preconnect_pool_devices()
_log.info(f"管理后台: http://localhost:{WEB_PORT}/ (admin/admin123)") _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: try:
_run_server(WEB_HOST, WEB_PORT) _run_server(WEB_HOST, WEB_PORT)
finally: 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() mgr.shutdown()
device_discovery.shutdown() device_discovery.shutdown()
_stop_uiauto() _stop_uiauto()