Files
open-im-server/internal/task/clear_msg.go
T

171 lines
6.4 KiB
Go
Raw Normal View History

2023-02-23 18:20:45 +08:00
package task
2022-08-08 11:30:10 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/controller"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/utils"
2023-02-15 15:52:32 +08:00
"context"
2023-02-23 18:20:45 +08:00
"fmt"
"github.com/go-redis/redis/v8"
2022-08-10 19:31:57 +08:00
"math"
2022-08-08 11:30:10 +08:00
)
2023-02-23 18:20:45 +08:00
type msgTool struct {
2023-02-23 17:28:57 +08:00
msgInterface controller.MsgDatabase
2023-02-22 20:33:19 +08:00
userInterface controller.UserDatabase
groupInterface controller.GroupDatabase
2022-08-08 11:30:10 +08:00
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) getCronTaskOperationID() string {
2023-02-15 15:52:32 +08:00
return cronTaskOperationID + utils.OperationIDGenerator()
2022-08-10 15:14:51 +08:00
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) ClearAll() {
2023-02-15 15:52:32 +08:00
operationID := c.getCronTaskOperationID()
ctx := context.Background()
tracelog.SetOperationID(ctx, operationID)
2023-02-16 15:20:59 +08:00
log.NewInfo(operationID, "============================ start del cron task ============================")
2023-02-15 15:52:32 +08:00
var err error
2023-02-16 15:20:59 +08:00
userIDList, err := c.userInterface.GetAllUserID(ctx)
2023-02-15 15:52:32 +08:00
if err == nil {
2023-02-16 15:20:59 +08:00
c.ClearUsersMsg(ctx, userIDList)
2023-02-15 15:52:32 +08:00
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
// working group msg clear
2023-02-23 19:15:30 +08:00
superGroupIDList, err := c.groupInterface.GetGroupIDsByGroupType(ctx, constant.WorkingGroup)
2023-02-15 15:52:32 +08:00
if err == nil {
2023-02-23 19:15:30 +08:00
c.ClearSuperGroupMsg(ctx, superGroupIDList)
2023-02-15 15:52:32 +08:00
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
2023-02-16 15:20:59 +08:00
log.NewInfo(operationID, "============================ start del cron finished ============================")
2022-11-04 16:18:45 +08:00
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) ClearUsersMsg(ctx context.Context, userIDList []string) {
2023-02-15 15:52:32 +08:00
for _, userID := range userIDList {
2023-02-20 10:13:29 +08:00
if err := c.msgInterface.DeleteUserMsgsAndSetMinSeq(ctx, userID, int64(config.Config.Mongo.DBRetainChatRecords*24*60*60)); err != nil {
2023-02-16 15:20:59 +08:00
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), userID)
2022-08-10 15:14:51 +08:00
}
2023-02-20 10:13:29 +08:00
_, maxSeqMongo, minSeqCache, maxSeqCache, err := c.msgInterface.GetUserMinMaxSeqInMongoAndCache(ctx, userID)
2023-02-16 15:20:59 +08:00
if err != nil {
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), "GetUserMinMaxSeqInMongoAndCache failed", userID)
continue
2022-11-03 18:31:29 +08:00
}
2023-02-20 10:13:29 +08:00
c.FixUserSeq(ctx, userID, minSeqCache, maxSeqCache)
c.CheckMaxSeqWithMongo(ctx, userID, maxSeqCache, maxSeqMongo, constant.WriteDiffusion)
2022-11-01 19:39:56 +08:00
}
2023-02-15 15:52:32 +08:00
}
2023-02-23 19:15:30 +08:00
func (c *msgTool) ClearSuperGroupMsg(ctx context.Context, superGroupIDList []string) {
for _, groupID := range superGroupIDList {
2023-02-16 15:20:59 +08:00
userIDs, err := c.groupInterface.FindGroupMemberUserID(ctx, groupID)
2022-12-26 10:25:42 +08:00
if err != nil {
2023-02-16 15:20:59 +08:00
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), "FindGroupMemberUserID", err.Error(), groupID)
2023-02-15 15:52:32 +08:00
continue
2022-12-26 10:25:42 +08:00
}
2023-02-20 10:13:29 +08:00
if err := c.msgInterface.DeleteUserSuperGroupMsgsAndSetMinSeq(ctx, groupID, userIDs, int64(config.Config.Mongo.DBRetainChatRecords*24*60*60)); err != nil {
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), "DeleteUserSuperGroupMsgsAndSetMinSeq failed", groupID, userIDs, config.Config.Mongo.DBRetainChatRecords)
2022-11-03 19:50:06 +08:00
}
2023-02-20 10:13:29 +08:00
_, maxSeqMongo, maxSeqCache, err := c.msgInterface.GetSuperGroupMinMaxSeqInMongoAndCache(ctx, groupID)
if err != nil {
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), "GetUserMinMaxSeqInMongoAndCache failed", groupID)
continue
}
2023-02-23 18:20:45 +08:00
for _, userID := range userIDs {
minSeqCache, err := c.msgInterface.GetGroupUserMinSeq(ctx, groupID, userID)
if err != nil {
log.NewError(tracelog.GetOperationID(ctx), "GetGroupUserMinSeq failed", groupID, userID)
continue
}
c.FixGroupUserSeq(ctx, userID, groupID, minSeqCache, maxSeqCache)
}
2023-02-20 10:13:29 +08:00
c.CheckMaxSeqWithMongo(ctx, groupID, maxSeqCache, maxSeqMongo, constant.WriteDiffusion)
2022-11-03 19:50:06 +08:00
}
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) FixUserSeq(ctx context.Context, userID string, minSeqCache, maxSeqCache int64) {
2023-02-20 10:13:29 +08:00
if minSeqCache > maxSeqCache {
if err := c.msgInterface.SetUserMinSeq(ctx, userID, maxSeqCache); err != nil {
log.NewError(tracelog.GetOperationID(ctx), "SetUserMinSeq failed", userID, minSeqCache, maxSeqCache)
} else {
log.NewWarn(tracelog.GetOperationID(ctx), "SetUserMinSeq success", userID, minSeqCache, maxSeqCache)
2022-08-11 12:09:44 +08:00
}
2022-08-10 19:31:57 +08:00
}
2023-02-20 10:13:29 +08:00
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) FixGroupUserSeq(ctx context.Context, userID string, groupID string, minSeqCache, maxSeqCache int64) {
2023-02-20 10:13:29 +08:00
if minSeqCache > maxSeqCache {
if err := c.msgInterface.SetGroupUserMinSeq(ctx, groupID, userID, maxSeqCache); err != nil {
log.NewError(tracelog.GetOperationID(ctx), "SetGroupUserMinSeq failed", userID, minSeqCache, maxSeqCache)
} else {
log.NewWarn(tracelog.GetOperationID(ctx), "SetGroupUserMinSeq success", userID, minSeqCache, maxSeqCache)
}
2022-11-03 19:26:41 +08:00
}
2023-02-20 10:13:29 +08:00
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) CheckMaxSeqWithMongo(ctx context.Context, sourceID string, maxSeqCache, maxSeqMongo int64, diffusionType int) {
2023-02-20 10:13:29 +08:00
if math.Abs(float64(maxSeqMongo-maxSeqCache)) > 10 {
log.NewWarn(tracelog.GetOperationID(ctx), "cache max seq and mongo max seq is diff > 10", sourceID, maxSeqCache, maxSeqMongo, diffusionType)
2022-08-10 19:31:57 +08:00
}
}
2023-02-23 18:20:45 +08:00
func (c *msgTool) FixAllSeq(ctx context.Context) {
userIDs, err := c.userInterface.GetAllUserID(ctx)
if err != nil {
panic(err.Error())
}
for _, userID := range userIDs {
userCurrentMinSeq, err := c.msgInterface.GetUserMinSeq(ctx, userID)
if err != nil && err != redis.Nil {
continue
}
userCurrentMaxSeq, err := c.msgInterface.GetUserMaxSeq(ctx, userID)
if err != nil && err != redis.Nil {
continue
}
if userCurrentMinSeq > userCurrentMaxSeq {
if err = c.msgInterface.SetUserMinSeq(ctx, userID, userCurrentMaxSeq); err != nil {
fmt.Println("SetUserMinSeq failed", userID, userCurrentMaxSeq)
}
fmt.Println("fix", userID, userCurrentMaxSeq)
}
}
fmt.Println("fix users seq success")
groupIDs, err := c.groupInterface.GetGroupIDsByGroupType(ctx, constant.WorkingGroup)
if err != nil {
panic(err.Error())
}
for _, groupID := range groupIDs {
maxSeq, err := c.msgInterface.GetGroupMaxSeq(ctx, groupID)
if err != nil {
fmt.Println("GetGroupMaxSeq failed", groupID)
continue
}
userIDs, err := c.groupInterface.FindGroupMemberUserID(ctx, groupID)
if err != nil {
fmt.Println("get groupID", groupID, "failed, try again later")
continue
}
for _, userID := range userIDs {
userMinSeq, err := c.msgInterface.GetGroupUserMinSeq(ctx, groupID, userID)
if err != nil && err != redis.Nil {
fmt.Println("GetGroupUserMinSeq failed", groupID, userID)
continue
}
if userMinSeq > maxSeq {
if err = c.msgInterface.SetGroupUserMinSeq(ctx, groupID, userID, maxSeq); err != nil {
fmt.Println("SetGroupUserMinSeq failed", err.Error(), groupID, userID, maxSeq)
}
fmt.Println("fix", groupID, userID, maxSeq, userMinSeq)
}
}
}
fmt.Println("fix all seq finished")
}