66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package queues
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"hotgo/internal/consts"
|
|
"hotgo/internal/library/imkafka"
|
|
"hotgo/internal/library/queue"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
func init() {
|
|
queue.RegisterConsumer(ImDeadLetter)
|
|
}
|
|
|
|
var ImDeadLetter = &qImDeadLetter{}
|
|
|
|
type qImDeadLetter struct{}
|
|
|
|
func (q *qImDeadLetter) GetTopic() string {
|
|
return consts.QueueImDeadLetterTopic
|
|
}
|
|
|
|
func (q *qImDeadLetter) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
|
var envelope imkafka.Envelope
|
|
if err = json.Unmarshal(mqMsg.Body, &envelope); err != nil {
|
|
return err
|
|
}
|
|
payload := map[string]any{}
|
|
if len(envelope.Payload) > 0 {
|
|
_ = json.Unmarshal(envelope.Payload, &payload)
|
|
}
|
|
_, err = g.DB().Exec(ctx, `INSERT IGNORE INTO im_kafka_dead_letter_events
|
|
(event_id,parent_event_id,tenant_code,event_type,biz_type,biz_id,source_service,target_service,dead_id,reason,message,payload,envelope,received_at)
|
|
VALUES(?,?,?,?,?,?,?,?,?,?,?,CAST(NULLIF(?, '') AS JSON),CAST(? AS JSON),?)`,
|
|
envelope.EventID, envelope.ParentEventID, envelope.TenantCode, envelope.EventType, envelope.BizType, envelope.BizID,
|
|
envelope.SourceService, envelope.TargetService, stringValue(payload["dead_id"]),
|
|
firstString(payload["reason"], payload["last_reason"]), firstString(payload["message"], payload["last_error"]),
|
|
string(envelope.Payload), string(mqMsg.Body), gtime.Now())
|
|
return err
|
|
}
|
|
|
|
func stringValue(v any) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
switch value := v.(type) {
|
|
case string:
|
|
return value
|
|
default:
|
|
b, _ := json.Marshal(value)
|
|
return string(b)
|
|
}
|
|
}
|
|
|
|
func firstString(values ...any) string {
|
|
for _, v := range values {
|
|
if text := stringValue(v); text != "" && text != "null" {
|
|
return text
|
|
}
|
|
}
|
|
return ""
|
|
}
|