修复redis和queueprocessor
This commit is contained in:
@@ -180,9 +180,10 @@ func AddToStream(ctx context.Context, streamKey string, msg interface{}) (messag
|
||||
}
|
||||
|
||||
// CreateConsumerGroup 创建消费者组(如果不存在)
|
||||
// XGROUP CREATE streamKey groupName $ MKSTREAM
|
||||
// XGROUP CREATE streamKey groupName 0 MKSTREAM
|
||||
// 使用0作为起始ID,从Stream开头读取所有未消费消息
|
||||
func CreateConsumerGroup(ctx context.Context, streamKey, groupName string) error {
|
||||
_, err := redisClient.Do(ctx, "XGROUP", "CREATE", streamKey, groupName, "$", "MKSTREAM")
|
||||
_, err := redisClient.Do(ctx, "XGROUP", "CREATE", streamKey, groupName, "0", "MKSTREAM")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -205,11 +206,29 @@ RECONNECT:
|
||||
"STREAMS", streamKey, "0", // ID=0 读取pending消息
|
||||
)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "❌ XREADGROUP读取pending失败: stream=%s, error=%v", streamKey, err)
|
||||
time.Sleep(time.Second)
|
||||
goto RECONNECT
|
||||
}
|
||||
|
||||
// 检查pending结果是否为空(需要检查消息数组是否为空)
|
||||
hasPending := false
|
||||
if result != nil && !result.IsEmpty() {
|
||||
// 尝试解析map格式
|
||||
if resultVal := result.Val(); resultVal != nil {
|
||||
if streamsMap, ok := resultVal.(map[interface{}]interface{}); ok {
|
||||
for _, streamMsgs := range streamsMap {
|
||||
if msgsArray, ok := streamMsgs.([]interface{}); ok && len(msgsArray) > 0 {
|
||||
hasPending = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有pending消息,读取新消息
|
||||
if result == nil || result.IsEmpty() {
|
||||
if !hasPending {
|
||||
result, err = redisClient.Do(execCtx,
|
||||
"XREADGROUP", "GROUP", groupName, consumerName,
|
||||
"COUNT", count,
|
||||
@@ -217,6 +236,8 @@ RECONNECT:
|
||||
"STREAMS", streamKey, ">",
|
||||
)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "❌ XREADGROUP读取新消息失败: stream=%s, error=%v", streamKey, err)
|
||||
time.Sleep(time.Second)
|
||||
goto RECONNECT
|
||||
}
|
||||
}
|
||||
@@ -234,19 +255,26 @@ RECONNECT:
|
||||
|
||||
// 尝试 map 格式(GoFrame gredis 返回)
|
||||
if streamsMap, ok := resultVal.(map[interface{}]interface{}); ok {
|
||||
for _, streamMsgs := range streamsMap {
|
||||
for streamKey, streamMsgs := range streamsMap {
|
||||
msgsArray, ok := streamMsgs.([]interface{})
|
||||
if !ok {
|
||||
g.Log().Errorf(ctx, "❌ streamMsgs类型转换失败: streamKey=%v, 实际类型=%T", streamKey, streamMsgs)
|
||||
continue
|
||||
}
|
||||
for _, msgData := range msgsArray {
|
||||
for i, msgData := range msgsArray {
|
||||
msgArray, ok := msgData.([]interface{})
|
||||
if !ok || len(msgArray) < 2 {
|
||||
if !ok {
|
||||
g.Log().Errorf(ctx, "❌ msgData类型转换失败: index=%d, 实际类型=%T", i, msgData)
|
||||
continue
|
||||
}
|
||||
if len(msgArray) < 2 {
|
||||
g.Log().Errorf(ctx, "❌ msgArray长度不足: index=%d, len=%d", i, len(msgArray))
|
||||
continue
|
||||
}
|
||||
msgID := gconv.String(msgArray[0])
|
||||
fieldsArray, ok := msgArray[1].([]interface{})
|
||||
if !ok {
|
||||
g.Log().Errorf(ctx, "❌ fieldsArray类型转换失败: msgID=%s, msgArray[1]类型=%T", msgID, msgArray[1])
|
||||
continue
|
||||
}
|
||||
values := make(map[string]interface{}, len(fieldsArray)/2)
|
||||
@@ -262,6 +290,9 @@ RECONNECT:
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
g.Log().Errorf(ctx, "❌ [ReadFromStream] map格式解析失败: streamsMap长度=%d, 但未提取到消息", len(streamsMap))
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
@@ -299,8 +330,13 @@ RECONNECT:
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
g.Log().Errorf(ctx, "❌ [ReadFromStream] 数组格式解析失败: streamsArray长度=%d, 但未提取到消息", len(streamsArray))
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
g.Log().Errorf(ctx, "❌ [ReadFromStream] 无法识别的result格式, resultVal类型: %T, 值: %+v", resultVal, resultVal)
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user