sync: 对齐小程序 v1.0.2 生产代码并脱敏
This commit is contained in:
@@ -3,6 +3,7 @@ package rabbitmq
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -41,6 +42,25 @@ func Publish(ctx context.Context, queue string, data any) error {
|
||||
return PublishBytes(ctx, queue, body)
|
||||
}
|
||||
|
||||
func PublishDelayed(ctx context.Context, delayQueue, targetQueue string, delay time.Duration, data any) error {
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return gerror.Wrap(err, "序列化延时工单消息失败")
|
||||
}
|
||||
ch, err := channel(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ch.Close()
|
||||
if _, err = ch.QueueDeclare(targetQueue, true, false, false, false, nil); err != nil {
|
||||
return gerror.Wrap(err, "声明工单目标队列失败")
|
||||
}
|
||||
if _, err = ch.QueueDeclare(delayQueue, true, false, false, false, amqp.Table{"x-dead-letter-exchange": "", "x-dead-letter-routing-key": targetQueue}); err != nil {
|
||||
return gerror.Wrap(err, "声明工单延时队列失败")
|
||||
}
|
||||
return gerror.Wrap(ch.PublishWithContext(ctx, "", delayQueue, false, false, amqp.Publishing{ContentType: "application/json", DeliveryMode: amqp.Persistent, Expiration: fmt.Sprintf("%d", delay.Milliseconds()), Timestamp: time.Now(), Body: body}), "发送工单延时消息失败")
|
||||
}
|
||||
|
||||
func PublishBytes(ctx context.Context, queue string, body []byte) error {
|
||||
if queue == "" {
|
||||
return gerror.New("RabbitMQ 队列名不能为空")
|
||||
|
||||
Reference in New Issue
Block a user