feat: ETCD 备份 + 邮件/飞书通知

ETCD 备份(新增类型):
- 后端 core/backup/etcd.py:调用 etcdctl snapshot save(v3.5.17 二进制)
  生成快照文件,与 META.txt 一起 tar.gz 流式打包上传。
- 配置支持 endpoints(逗号分隔多节点)、basic auth、TLS(cacert/cert/key)。
- Dockerfile 下载 etcdctl 二进制(绕过 apt 签名问题)。

通知模块:
- 核心 app/core/notify.py:
  - 邮件:smtplib + MIMEMultipart(线程池里跑避免阻塞)
  - 飞书:httpx 调自定义机器人 webhook,发 interactive card
- settings API:
  - GET/PUT /api/settings/notifications(密码脱敏 *** 编辑占位)
  - POST /api/settings/notifications/test 测试通道
- Job 增加 notify_on 字段(none/failure/all),Alembic 0002 迁移。
- executor 在 run 完成后根据 job.notify_on 与 run.status 异步触发通知,
  重新加载最新 run 信息生成上下文。

前端:
- JobForm 加 ETCD 表单字段与 notify_on 选择器;type 选项加 etcd。
- JobList 显示通知策略徽标。
- Settings 页加通知配置卡片:邮件(折叠面板 + 测试按钮)+ 飞书 + 保存。
- 新增 api/settings.ts。
- 类型扩展:EtcdSourceConfig、NotificationsConfig、SmtpConfig。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
publ
2026-06-23 11:24:53 +08:00
parent 5313bc9a3a
commit 07b2016f33
15 changed files with 996 additions and 14 deletions
+60
View File
@@ -14,6 +14,7 @@ from sqlalchemy.orm import Session
from app.core.backup.base import BaseBackup
from app.core.backup.directory import DirectoryBackup
from app.core.backup.etcd import EtcdBackup
from app.core.backup.influxdb import InfluxDBBackup
from app.core.backup.mysql import MySQLBackup
from app.core.retention import apply_retention
@@ -35,6 +36,8 @@ def _make_backup(job: Job) -> BaseBackup:
return DirectoryBackup(src, job.name)
if job.type == "influxdb":
return InfluxDBBackup(src, job.name)
if job.type == "etcd":
return EtcdBackup(src, job.name)
raise ValueError(f"Unsupported job type: {job.type}")
@@ -127,6 +130,63 @@ async def run_backup(run_id: int, trigger: str = "manual") -> None:
db.add(run_log)
db.commit()
log.info("Backup run_id=%s finished status=%s", run_id, run.status)
# 触发通知(在事务外,避免阻塞 executorfire and forget
try:
should_notify = (
job.notify_on == "all"
or (job.notify_on == "failure" and run.status == "failed")
)
if should_notify:
asyncio.create_task(_dispatch_notification(job.id, run.id))
except Exception as e:
log.warning("Failed to schedule notification: %s", e)
finally:
db.close()
async def _dispatch_notification(job_id: int, run_id: int) -> None:
"""从 DB 重新加载最新 run 状态后发通知。"""
from app.core.notify import NotificationContext, send_notifications
from app.models.setting import Setting
from app.utils.crypto import decrypt_dict
db = SessionLocal()
try:
job = db.get(Job, job_id)
run = db.get(Run, run_id)
if not job or not run:
return
notify_row = db.get(Setting, "notifications")
if not notify_row or not notify_row.value:
return
try:
settings = decrypt_dict(notify_row.value)
except Exception:
return
if not (settings.get("email_enabled") or settings.get("feishu_enabled")):
return
ctx = NotificationContext(
job_name=job.name,
job_type=job.type,
status=run.status,
run_id=run.id,
artifact_size=run.artifact_size,
duration_seconds=run.duration_seconds,
error_message=run.error_message,
artifact_path=run.artifact_path,
started_at=run.started_at.isoformat() if run.started_at else None,
finished_at=run.finished_at.isoformat() if run.finished_at else None,
)
result = await send_notifications(settings, ctx)
log.info(
"Notification dispatched for run_id=%s: email_ok=%s feishu_ok=%s",
run.id, result.email_ok, result.feishu_ok,
)
finally:
db.close()