feat(qa): 周报确认后按设定时段自动发邮件
build / build (push) Successful in 1m23s

周报页新增自动发送区:管理员配置 SMTP/收件人/发送星期与小时,授权码用主密钥加密保存。
确认绑定当前草稿版本与正文哈希,正文再改则确认失效;worker 每分钟仅在启用、到点、周已结束且未发送过时发一次。
邮件同时提供渲染后的 HTML 与可读纯文本;SMTP 仅允许 TLS,拒绝明文。
迁移 000060 新增设置、确认与发送记录三张表,唯一键防重复发送。
This commit is contained in:
hermes
2026-10-26 09:49:23 +08:00
parent 1d33ff6976
commit 6816289552
18 changed files with 1550 additions and 2 deletions
+1
View File
@@ -77,6 +77,7 @@ func run(ctx context.Context, getenv func(string) string) error {
if mirrorStore != nil {
go runRecordsSyncScheduler(ctx, mirrorStore, func(error) { log.Print("QA local mirror scheduler unavailable; last committed data retained") })
}
go weeklyMailLoop(ctx, pool, getenv)
go runSourceRefreshScheduler(ctx, store, config.SourceRefreshSchedulerInterval, func(err error) {
log.Printf("worker %s source refresh scheduler error: %v", config.WorkerID, err)
})
+43
View File
@@ -0,0 +1,43 @@
package main
import (
"context"
"log"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"knowledge-governance/internal/qawemail"
)
// weeklyMailLoop sends the confirmed weekly report exactly inside the configured slot.
// It runs in the ordinary worker so the console never depends on a browser staying open.
func weeklyMailLoop(ctx context.Context, pool *pgxpool.Pool, getenv func(string) string) {
tenant := getenv("DOBOTAI_OPS_TENANT_KEY")
if tenant == "" {
log.Print("weekly mail disabled: tenant key not configured")
return
}
store := qawemail.NewStore(pool, tenant, getenv("QA_AI_MASTER_KEY_FILE"), nil)
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
lastState := ""
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
runCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
result, err := store.SendDue(runCtx, time.Now())
cancel()
if err != nil {
log.Printf("weekly mail attempt failed: %v", err)
continue
}
if result.State != lastState {
lastState = result.State
log.Printf("weekly mail state=%s week=%s reason=%s", result.State, result.Week, result.Reason)
}
}
}
}