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) } } } }