27da842a20
关闭 notify_* 后通过 Redis 广播同步各 worker 内存设置;修复 expanded id 类型不匹配导致点击名称无法展开详情。 Co-authored-by: Cursor <cursoragent@cursor.com>
2.0 KiB
2.0 KiB
2026-06-08 — 告警 Telegram 开关多 Worker 同步
摘要
修复 Settings 关闭「CPU/内存/磁盘等资源告警 Telegram 通知」后仍收到 Telegram 的问题。根因:多 uvicorn worker 下仅处理 PUT 的进程热更新了内存 settings,处理 Agent 心跳的其他 worker 仍认为开关为开启。
动机
用户关闭「CPU 超阈值告警」等 notify_* 开关后 Telegram 仍推送;设置页文案已说明「关闭后不发送 Telegram」,但生产 --workers 4 时行为不一致。
变更
server/config.py— 新增apply_db_override(db_key, value),统一单条设置热更新逻辑。server/infrastructure/settings_broadcast.py— 新增 Redis 频道nexus:settings;PUT 后 publish,各 worker 订阅并apply_db_override。server/main.py— lifespan 启动/停止 settings subscriber。server/api/settings.py—set_setting热更新后调用publish_setting_change。server/utils/notify_toggles.py— 集中notify_enabled/resource_alert_notify_enabled等 helper。server/infrastructure/telegram/__init__.py— 全部 Telegram 发送入口复用 notify helper(行为不变,逻辑集中)。tests/test_notify_toggle_sync.py— 开关解析、Telegram 跳过、广播 publish 测试。
涉及文件
server/config.pyserver/infrastructure/settings_broadcast.pyserver/main.pyserver/api/settings.pyserver/utils/notify_toggles.pyserver/infrastructure/telegram/__init__.pytests/test_notify_toggle_sync.py
迁移 / 重启
- 无 DB 迁移。
- 需重启后端(或部署)以加载 settings subscriber。
验证
pytest tests/test_notify_toggle_sync.py tests/test_settings_telegram.py -q
- Settings 关闭 CPU 告警 → 任意 worker 处理心跳时
send_telegram_alert返回 False,不调用 Telegram API。 - WebSocket 告警与
alert_logs仍记录(与设置页说明一致)。