修复并发,暂时的,不够完整,后续还是需要重新设计,这个太乱了,傻逼AI
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m22s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m22s
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -38,6 +39,12 @@ func NewClient(redisClient *redis.Client, logger *zap.Logger) *Client {
|
||||
// payload 必须传入 struct 或 map,禁止传入 []byte。
|
||||
// 传入 []byte 会导致 sonic.Marshal 将其 base64 编码,handler 解析时类型不匹配。
|
||||
func (c *Client) EnqueueTask(ctx context.Context, taskType string, payload interface{}, opts ...asynq.Option) error {
|
||||
if _, ok := payload.([]byte); ok {
|
||||
c.logger.Error("任务载荷类型错误,禁止传入 []byte",
|
||||
zap.String("task_type", taskType))
|
||||
return fmt.Errorf("task payload must be struct or map, got []byte")
|
||||
}
|
||||
|
||||
// 内部统一序列化,调用方无需预先 Marshal
|
||||
payloadBytes, err := sonic.Marshal(payload)
|
||||
if err != nil {
|
||||
@@ -47,6 +54,8 @@ func (c *Client) EnqueueTask(ctx context.Context, taskType string, payload inter
|
||||
return fmt.Errorf("failed to marshal task payload: %w", err)
|
||||
}
|
||||
|
||||
opts = append(opts, asynq.Queue(constants.QueueForTaskType(taskType)))
|
||||
|
||||
// 创建任务
|
||||
task := asynq.NewTask(taskType, payloadBytes, opts...)
|
||||
|
||||
@@ -78,13 +87,12 @@ func (c *Client) Close() error {
|
||||
|
||||
// ParseQueueConfig 解析队列配置为 Asynq 格式
|
||||
func ParseQueueConfig(cfg *config.QueueConfig) map[string]int {
|
||||
queues := constants.DefaultTaskQueueWeights()
|
||||
if cfg.Queues != nil && len(cfg.Queues) > 0 {
|
||||
return cfg.Queues
|
||||
}
|
||||
// 默认队列优先级
|
||||
return map[string]int{
|
||||
"critical": 6,
|
||||
"default": 3,
|
||||
"low": 1,
|
||||
// 配置只覆盖权重;任务队列全集由代码兜底,避免漏监听新队列。
|
||||
for queueName, weight := range cfg.Queues {
|
||||
queues[queueName] = weight
|
||||
}
|
||||
}
|
||||
return queues
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user