78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.xiaotuo.co/micro/skeleton/internal/queue/jetstream"
|
|
"git.xiaotuo.co/pkg/go-plugins/logger"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
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")
|
|
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
|
|
}
|