This commit is contained in:
1 parent
b50e5bdfa2
commit
dd6258c98b
34 files changed
+734
-87
No files matched your search
@@ -2,13 +2,47 @@ package logstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MailDetails struct {
|
||||
Trigger string `json:"trigger"`
|
||||
Stage string `json:"stage"`
|
||||
Outcome string `json:"outcome"`
|
||||
AcceptedCount int `json:"acceptedCount"`
|
||||
}
|
||||
|
||||
// MailFailureReason 使用错误类别给出原因,不保存可能携带凭据的远端响应原文。
|
||||
func MailFailureReason(err error) string {
|
||||
var certificate *tls.CertificateVerificationError
|
||||
var dns *net.DNSError
|
||||
var protocol *textproto.Error
|
||||
var network net.Error
|
||||
switch {
|
||||
case errors.As(err, &certificate):
|
||||
return "对端证书验证失败"
|
||||
case errors.As(err, &dns):
|
||||
return "收件域名解析失败"
|
||||
case errors.As(err, &protocol):
|
||||
return "对端 SMTP 拒绝,状态码 " + strconv.Itoa(protocol.Code)
|
||||
case errors.As(err, &network):
|
||||
if network.Timeout() {
|
||||
return "投递连接超时"
|
||||
}
|
||||
return "投递网络连接失败"
|
||||
default:
|
||||
return "投递未完成,请结合运行日志排查"
|
||||
}
|
||||
}
|
||||
|
||||
// MailResult 记录投递尝试元数据,不复制正文、附件、认证信息或服务器错误原文。
|
||||
func MailResult(userID int, direction string, sender string, recipientCount, size int, result string) {
|
||||
func MailResult(userID int, direction string, sender string, recipientCount, size int, result string, details ...MailDetails) {
|
||||
if len(sender) > 320 {
|
||||
sender = "地址超出显示长度"
|
||||
}
|
||||
@@ -17,7 +51,8 @@ func MailResult(userID int, direction string, sender string, recipientCount, siz
|
||||
Sender string `json:"sender"`
|
||||
RecipientCount int `json:"recipientCount"`
|
||||
Bytes int `json:"bytes"`
|
||||
}{direction, sender, recipientCount, size})
|
||||
MailDetails
|
||||
}{direction, sender, recipientCount, size, mailDetails(direction, result, details)})
|
||||
action := "接收邮件"
|
||||
if direction == "outbound" {
|
||||
action = "投递邮件"
|
||||
@@ -29,3 +64,23 @@ func MailResult(userID int, direction string, sender string, recipientCount, siz
|
||||
Kind: "mail", ActorID: strconv.Itoa(userID), CreatedAt: time.Now(), Action: action, Result: result, Payload: payload,
|
||||
})
|
||||
}
|
||||
|
||||
func mailDetails(direction, result string, details []MailDetails) MailDetails {
|
||||
value := MailDetails{Trigger: "SMTP 收信", Stage: "接收处理"}
|
||||
if direction == "outbound" {
|
||||
value.Trigger, value.Stage = "邮件发送", "对外投递"
|
||||
} else if direction == "submission" {
|
||||
value.Trigger, value.Stage = "SMTP 客户端提交", "提交处理"
|
||||
}
|
||||
if len(details) > 0 {
|
||||
value = details[0]
|
||||
}
|
||||
if value.Outcome == "" {
|
||||
value.Outcome = map[string]string{
|
||||
"success": "当前处理阶段已完成", "accepted": "对端服务器已接收,不代表收件人已阅读或最终入箱",
|
||||
"failure": "当前处理阶段失败", "partial": "部分收件人已被对端接收,其余投递失败",
|
||||
"discarded": "已按收信策略丢弃,未写入邮箱", "unknown": "处理未正常完成,结果待确认",
|
||||
}[result]
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -23,7 +23,7 @@ func Record(ctx context.Context, entry Entry) {
|
||||
bounded, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := store.Write(bounded, entry); err != nil {
|
||||
log.WithError(err).Error("业务日志写入失败")
|
||||
log.WithError(err).WithFields(log.Fields{"action": "写入业务日志", "object": entry.Kind, "result": "failure"}).Error("业务日志写入失败")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func Start(store *Store) *Runtime {
|
||||
_, err := store.Cleanup(bounded, time.Now())
|
||||
stop()
|
||||
if err != nil && ctx.Err() == nil {
|
||||
log.WithError(err).Error("过期业务日志清理失败,将在下一轮重试")
|
||||
log.WithError(err).WithFields(log.Fields{"action": "清理过期业务日志", "result": "failure"}).Error("过期业务日志清理失败,将在下一轮重试")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
||||
Reference in new issue
Block a user