41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/contract"
|
|
"git.ipao.vip/rogee/go-sip/internal/mq"
|
|
)
|
|
|
|
func (d *Dispatcher) ConsumeTenant(ctx context.Context, broker *mq.Broker, tenantKey string) error {
|
|
if broker == nil {
|
|
return errors.New("broker is required")
|
|
}
|
|
if err := contract.ValidateTenantKey(tenantKey); err != nil {
|
|
return err
|
|
}
|
|
queue, err := broker.DeclareTenantQueue(tenantKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return broker.Consume(ctx, queue, func(ctx context.Context, routingKey string, body []byte) error {
|
|
if _, err := d.AcceptCommand(body, routingKey); err != nil {
|
|
if isPermanentCommandError(err) {
|
|
return mq.Permanent(err)
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func isPermanentCommandError(err error) bool {
|
|
if errors.Is(err, contract.ErrInvalidTenantKey) {
|
|
return true
|
|
}
|
|
message := err.Error()
|
|
return strings.Contains(message, "schema validation") || strings.Contains(message, "decode json") || strings.Contains(message, "routing mismatch")
|
|
}
|