feat: fulfillment modes, MQ, admin status, docs; scrub compose secrets

Made-with: Cursor
This commit is contained in:
2026-04-03 16:26:38 +08:00
parent 1f16966174
commit b4c5103fb3
56 changed files with 4088 additions and 3742 deletions

View File

@@ -0,0 +1,268 @@
package mq
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"mengyastore-backend/internal/storage"
)
// Client manages a single AMQP connection, a publish channel, and naming for one env.
type Client struct {
conn *amqp.Connection
pubCh *amqp.Channel
exchange string
queue string
env string
amqpURL string
mu sync.Mutex
closing sync.Once
connected bool
// 用于重连后重启消费协程(与 StartConsumer 注入的一致)
consumerCtx context.Context
consumerSite *storage.SiteStore
}
// New connects to RabbitMQ and declares exchange + queue + binding (idempotent).
func New(amqpURL, env string) (*Client, error) {
if amqpURL == "" {
return nil, fmt.Errorf("empty amqp url")
}
conn, err := amqp.DialConfig(amqpURL, amqp.Config{
Heartbeat: 30 * time.Second,
Locale: "en_US",
})
if err != nil {
return nil, fmt.Errorf("amqp dial: %w", err)
}
pubCh, err := conn.Channel()
if err != nil {
conn.Close()
return nil, fmt.Errorf("amqp channel: %w", err)
}
exchange, queue, err := declareTopology(pubCh, env)
if err != nil {
pubCh.Close()
conn.Close()
return nil, err
}
return &Client{
conn: conn,
pubCh: pubCh,
exchange: exchange,
queue: queue,
env: sanitizeEnv(env),
amqpURL: amqpURL,
connected: true,
}, nil
}
func isRecoverableAMQP(err error) bool {
if err == nil {
return false
}
var e *amqp.Error
if errors.As(err, &e) {
// 504 channel/connection not open、320 连接被服务端关闭等,通过重连恢复
if e.Code == amqp.ChannelError || e.Code == amqp.ConnectionForced {
return true
}
return strings.Contains(strings.ToLower(e.Reason), "not open")
}
s := strings.ToLower(err.Error())
return strings.Contains(s, "channel/connection is not open") ||
strings.Contains(s, "connection closed") ||
strings.Contains(s, "use of closed network connection") ||
strings.Contains(s, "eof")
}
// reconnectLocked 在持有 mu 时调用:关闭旧连接并重新拨号、声明拓扑。
func (c *Client) reconnectLocked() error {
if !c.connected || c.amqpURL == "" {
return fmt.Errorf("mq client closed")
}
if c.pubCh != nil {
_ = c.pubCh.Close()
c.pubCh = nil
}
if c.conn != nil && !c.conn.IsClosed() {
_ = c.conn.Close()
}
c.conn = nil
conn, err := amqp.DialConfig(c.amqpURL, amqp.Config{
Heartbeat: 30 * time.Second,
Locale: "en_US",
})
if err != nil {
return fmt.Errorf("amqp reconnect dial: %w", err)
}
pubCh, err := conn.Channel()
if err != nil {
conn.Close()
return fmt.Errorf("amqp reconnect channel: %w", err)
}
exchange, queue, err := declareTopology(pubCh, c.env)
if err != nil {
pubCh.Close()
conn.Close()
return err
}
c.conn = conn
c.pubCh = pubCh
c.exchange = exchange
c.queue = queue
if c.consumerCtx != nil && c.consumerSite != nil && c.consumerCtx.Err() == nil {
go RunOrderEmailConsumer(c.consumerCtx, c.conn, c.env, c.consumerSite)
log.Printf("[MQ] consumer restarted after reconnect (queue=%s)", c.queue)
}
return nil
}
func (c *Client) passiveQueueLocked() error {
if c.pubCh == nil || !c.connected {
return fmt.Errorf("mq client closed")
}
_, err := c.pubCh.QueueDeclarePassive(c.queue, true, false, false, false, nil)
return err
}
// PublishOrderEmail publishes a persistent JSON message to the order-email routing key.
func (c *Client) PublishOrderEmail(ctx context.Context, p OrderEmailPayload) error {
body, err := json.Marshal(p)
if err != nil {
return fmt.Errorf("marshal payload: %w", err)
}
c.mu.Lock()
defer c.mu.Unlock()
if c.pubCh == nil || !c.connected {
return fmt.Errorf("mq client closed")
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
tryPublish := func() error {
return c.pubCh.PublishWithContext(ctx,
c.exchange,
routingKeyOrderEmail,
false,
false,
amqp.Publishing{
ContentType: "application/json",
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
Body: body,
},
)
}
err = tryPublish()
if err != nil && isRecoverableAMQP(err) {
if rerr := c.reconnectLocked(); rerr == nil {
err = tryPublish()
} else {
err = fmt.Errorf("%w (reconnect: %v)", err, rerr)
}
}
if err != nil {
return fmt.Errorf("publish: %w", err)
}
return nil
}
// QueueInspectInfo returns current queue depth and consumer count (passive declare).
func (c *Client) QueueInspectInfo() (messages int, consumers int, err error) {
c.mu.Lock()
defer c.mu.Unlock()
try := func() (int, int, error) {
if c.pubCh == nil || !c.connected {
return 0, 0, fmt.Errorf("mq client closed")
}
q, e := c.pubCh.QueueDeclarePassive(c.queue, true, false, false, false, nil)
if e != nil {
return 0, 0, e
}
return q.Messages, q.Consumers, nil
}
msgs, cons, err := try()
if err != nil && isRecoverableAMQP(err) {
if rerr := c.reconnectLocked(); rerr != nil {
return 0, 0, fmt.Errorf("%w (reconnect: %v)", err, rerr)
}
return try()
}
return msgs, cons, err
}
// Ping checks that the publish channel can query the declared queue (liveness for /api/health).
func (c *Client) Ping() error {
c.mu.Lock()
defer c.mu.Unlock()
err := c.passiveQueueLocked()
if err != nil && isRecoverableAMQP(err) {
if rerr := c.reconnectLocked(); rerr != nil {
return fmt.Errorf("%w (reconnect: %v)", err, rerr)
}
return c.passiveQueueLocked()
}
return err
}
// Env returns the sanitized environment suffix used in exchange/queue names.
func (c *Client) Env() string { return c.env }
// StartConsumer runs the order-email consumer until ctx is cancelled (run in a goroutine).
func (c *Client) StartConsumer(ctx context.Context, site *storage.SiteStore) {
c.mu.Lock()
c.consumerCtx = ctx
c.consumerSite = site
conn := c.conn
env := c.env
c.mu.Unlock()
if conn == nil || site == nil {
return
}
RunOrderEmailConsumer(ctx, conn, env, site)
}
// Close releases the publish channel and connection.
func (c *Client) Close() {
c.closing.Do(func() {
c.mu.Lock()
defer c.mu.Unlock()
c.connected = false
c.consumerCtx = nil
c.consumerSite = nil
if c.pubCh != nil {
_ = c.pubCh.Close()
c.pubCh = nil
}
if c.conn != nil {
_ = c.conn.Close()
c.conn = nil
}
})
}

View File

@@ -0,0 +1,101 @@
package mq
import (
"context"
"encoding/json"
"log"
amqp "github.com/rabbitmq/amqp091-go"
"mengyastore-backend/internal/email"
"mengyastore-backend/internal/storage"
)
// RunOrderEmailConsumer runs until ctx is done. Must be started in its own goroutine.
func RunOrderEmailConsumer(ctx context.Context, conn *amqp.Connection, env string, site *storage.SiteStore) {
if conn == nil || site == nil {
return
}
ch, err := conn.Channel()
if err != nil {
log.Printf("[MQ] consumer channel: %v", err)
return
}
defer ch.Close()
if _, _, err := declareTopology(ch, env); err != nil {
log.Printf("[MQ] consumer declare topology: %v", err)
return
}
if err := ch.Qos(1, 0, false); err != nil {
log.Printf("[MQ] consumer qos: %v", err)
return
}
queue := QueueName(env)
const tag = "mengyastore-order-email"
msgs, err := ch.Consume(queue, tag, false, false, false, false, nil)
if err != nil {
log.Printf("[MQ] consume %s: %v", queue, err)
return
}
log.Printf("[MQ] consumer started queue=%s", queue)
for {
select {
case <-ctx.Done():
_ = ch.Cancel(tag, false)
log.Printf("[MQ] consumer stopped queue=%s", queue)
return
case d, ok := <-msgs:
if !ok {
return
}
handleDelivery(site, d)
}
}
}
func handleDelivery(site *storage.SiteStore, d amqp.Delivery) {
var payload OrderEmailPayload
if err := json.Unmarshal(d.Body, &payload); err != nil {
log.Printf("[MQ] bad message: %v", err)
_ = d.Ack(false)
return
}
if payload.ToEmail == "" {
_ = d.Ack(false)
return
}
cfg, err := site.GetSMTPConfig()
if err != nil || !cfg.IsConfiguredEmail() {
log.Printf("[MQ] skip email order=%s: smtp not configured", payload.OrderID)
_ = d.Ack(false)
return
}
emailCfg := email.Config{
SMTPHost: cfg.Host,
SMTPPort: cfg.Port,
From: cfg.Email,
Password: cfg.Password,
FromName: cfg.FromName,
}
data := payload.ToNotifyData()
if err := email.SendOrderNotify(emailCfg, data); err != nil {
log.Printf("[MQ] send email fail order=%s: %v", payload.OrderID, err)
if d.Redelivered {
log.Printf("[MQ] drop order=%s after failed redelivery", payload.OrderID)
_ = d.Ack(false)
return
}
_ = d.Nack(false, true)
return
}
log.Printf("[MQ] email ok order=%s to=%s", payload.OrderID, payload.ToEmail)
_ = d.Ack(false)
}

View File

@@ -0,0 +1,26 @@
package mq
import "mengyastore-backend/internal/email"
// OrderEmailPayload is the JSON body published to RabbitMQ (no SMTP secrets).
type OrderEmailPayload struct {
ToEmail string `json:"toEmail"`
ToName string `json:"toName"`
ProductName string `json:"productName"`
OrderID string `json:"orderId"`
Quantity int `json:"quantity"`
Codes []string `json:"codes"`
IsManual bool `json:"isManual"`
}
func (p OrderEmailPayload) ToNotifyData() email.OrderNotifyData {
return email.OrderNotifyData{
ToEmail: p.ToEmail,
ToName: p.ToName,
ProductName: p.ProductName,
OrderID: p.OrderID,
Quantity: p.Quantity,
Codes: p.Codes,
IsManual: p.IsManual,
}
}

View File

@@ -0,0 +1,71 @@
package mq
import (
"fmt"
"strings"
amqp "github.com/rabbitmq/amqp091-go"
)
const routingKeyOrderEmail = "order.email.notify"
// OrderEmailRoutingKey is the binding / publish key for order notification messages.
func OrderEmailRoutingKey() string { return routingKeyOrderEmail }
// ExchangeName returns the durable topic exchange for this app + env (isolation from other apps on same broker).
func ExchangeName(env string) string {
return fmt.Sprintf("ex.mengyastore.%s.events", sanitizeEnv(env))
}
// QueueName returns the durable order-email queue name for this env.
func QueueName(env string) string {
return fmt.Sprintf("q.mengyastore.%s.order_email", sanitizeEnv(env))
}
func sanitizeEnv(env string) string {
e := strings.TrimSpace(strings.ToLower(env))
if e == "prod" || e == "production" {
return "prod"
}
return "dev"
}
func declareTopology(ch *amqp.Channel, env string) (exchange, queue string, err error) {
exchange = ExchangeName(env)
queue = QueueName(env)
if err = ch.ExchangeDeclare(
exchange,
"topic",
true,
false,
false,
false,
nil,
); err != nil {
return "", "", fmt.Errorf("exchange declare: %w", err)
}
if _, err = ch.QueueDeclare(
queue,
true,
false,
false,
false,
nil,
); err != nil {
return "", "", fmt.Errorf("queue declare: %w", err)
}
if err = ch.QueueBind(
queue,
routingKeyOrderEmail,
exchange,
false,
nil,
); err != nil {
return "", "", fmt.Errorf("queue bind: %w", err)
}
return exchange, queue, nil
}