Files
2026-11-11 17:49:38 +08:00

82 lines
2.3 KiB
Go

package logic
import (
"context"
"errors"
"time"
log "github.com/sirupsen/logrus"
"micro_layout/internal/logger"
"micro_layout/internal/queue/jetstream"
)
type MessageLogic struct {
}
func newMessageLogic() MessageLogic {
return MessageLogic{}
}
func (lgc MessageLogic) jetStreamPublish(ctx context.Context, topic string, body []byte) (err error) {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
Debug("start")
if Resources == nil || Resources.JetStreamBroker == nil {
return errors.New("jetstream is disabled")
}
ack, err := Resources.JetStreamBroker.PublishMsg(jetstream.NewMsg(ctx, topic, body))
if err != nil {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
Errorf("failed: %v", err)
} else {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_send").
WithField("topic", topic).
WithField("body", string(body)).
WithField("ack_sequence", ack.Sequence).
WithField("ack_duplicate", ack.Duplicate).
Debugf("success")
}
return err
}
func (lgc MessageLogic) QueueReceiveStartLog(ctx context.Context, topic string, payload []byte, reply string) {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("body", string(payload)).
WithField("reply", reply).
Debug("start")
}
func (lgc MessageLogic) QueueReceiveEndLog(ctx context.Context, topic string, err error, startTime time.Time, notifyTime, maxTime time.Duration) {
runTime := time.Now().Unix() - startTime.Unix()
slow := false
if float64(runTime) >= notifyTime.Seconds() {
slow = true
}
if err != nil {
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("run_time", runTime).
WithField("max_time", maxTime.Seconds()).
WithField("slow", slow).
Errorf("failed: %v", err)
return
}
log.WithFields(logger.WithContext(ctx)).
WithField("keyword", "nats_receive").
WithField("topic", topic).
WithField("run_time", runTime).
WithField("max_time", maxTime.Seconds()).
WithField("slow", slow).
Debugf("success")
return
}