You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/tools/msg.go

133 lines
4.5 KiB

2 years ago
package tools
import (
"context"
2 years ago
"errors"
2 years ago
"fmt"
2 years ago
"math"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
)
type MsgTool struct {
2 years ago
msgDatabase controller.CommonMsgDatabase
conversationDatabase controller.ConversationDataBase
userDatabase controller.UserDatabase
groupDatabase controller.GroupDatabase
2 years ago
}
2 years ago
var errSeq = errors.New("cache max seq and mongo max seq is diff > 10")
2 years ago
func NewMsgTool(msgDatabase controller.CommonMsgDatabase, userDatabase controller.UserDatabase, groupDatabase controller.GroupDatabase) *MsgTool {
2 years ago
return &MsgTool{
msgDatabase: msgDatabase,
userDatabase: userDatabase,
groupDatabase: groupDatabase,
}
}
func InitMsgTool() (*MsgTool, error) {
rdb, err := cache.NewRedis()
if err != nil {
return nil, err
}
mongo, err := unrelation.NewMongo()
if err != nil {
return nil, err
}
db, err := relation.NewGormDB()
if err != nil {
return nil, err
}
2 years ago
userDB := relation.NewUserGorm(db)
2 years ago
msgDatabase := controller.InitCommonMsgDatabase(rdb, mongo.GetDatabase())
2 years ago
userDatabase := controller.NewUserDatabase(userDB, cache.NewUserCacheRedis(rdb, relation.NewUserGorm(db), cache.GetDefaultOpt()), tx.NewGorm(db))
2 years ago
groupDatabase := controller.InitGroupDatabase(db, rdb, mongo.GetDatabase())
msgTool := NewMsgTool(msgDatabase, userDatabase, groupDatabase)
return msgTool, nil
}
2 years ago
func (c *MsgTool) AllConversationClearMsgAndFixSeq() {
2 years ago
ctx := mcontext.NewCtx(utils.GetSelfFuncName())
2 years ago
log.ZInfo(ctx, "============================ start del cron task ============================")
2 years ago
conversationIDs, err := c.conversationDatabase.GetAllConversationIDs(ctx)
if err != nil {
log.ZError(ctx, "GetAllConversationIDs failed", err)
return
2 years ago
}
2 years ago
for _, conversationID := range conversationIDs {
conversationIDs = append(conversationIDs, utils.GetNotificationConversationIDByConversationID(conversationID))
2 years ago
}
2 years ago
c.ClearConversationsMsg(ctx, conversationIDs)
log.ZInfo(ctx, "============================ start del cron finished ============================")
2 years ago
}
2 years ago
func (c *MsgTool) ClearConversationsMsg(ctx context.Context, conversationIDs []string) {
for _, conversationID := range conversationIDs {
if err := c.msgDatabase.DeleteConversationMsgsAndSetMinSeq(ctx, conversationID, int64(config.Config.Mongo.DBRetainChatRecords*24*60*60)); err != nil {
log.ZError(ctx, "DeleteUserSuperGroupMsgsAndSetMinSeq failed", err, "conversationID", conversationID, "DBRetainChatRecords", config.Config.Mongo.DBRetainChatRecords)
2 years ago
}
2 years ago
if err := c.fixAndCheckSeq(ctx, conversationID); err != nil {
log.ZError(ctx, "fixSeq failed", err, "conversationID", conversationID)
2 years ago
}
}
}
2 years ago
func (c *MsgTool) checkMaxSeqWithMongo(ctx context.Context, conversationID string, maxSeqCache int64) error {
maxSeqMongo, _, err := c.msgDatabase.GetMongoMaxAndMinSeq(ctx, conversationID)
2 years ago
if err != nil {
return err
}
2 years ago
if math.Abs(float64(maxSeqMongo-maxSeqCache)) > 10 {
return errSeq
2 years ago
}
2 years ago
return nil
}
2 years ago
func (c *MsgTool) fixAndCheckSeq(ctx context.Context, conversationID string) error {
maxSeq, err := c.msgDatabase.GetMaxSeq(ctx, conversationID)
2 years ago
if err != nil {
2 years ago
return err
2 years ago
}
2 years ago
minSeq, err := c.msgDatabase.GetMinSeq(ctx, conversationID)
2 years ago
if err != nil {
2 years ago
return err
}
if minSeq > maxSeq {
if err = c.msgDatabase.SetMinSeq(ctx, conversationID, maxSeq); err != nil {
return err
2 years ago
}
}
2 years ago
if err := c.checkMaxSeqWithMongo(ctx, conversationID, maxSeq); err != nil {
return err
2 years ago
}
2 years ago
return nil
2 years ago
}
2 years ago
func (c *MsgTool) FixAllSeq(ctx context.Context) error {
2 years ago
conversationIDs, err := c.conversationDatabase.GetAllConversationIDs(ctx)
2 years ago
if err != nil {
2 years ago
log.ZError(ctx, "GetAllConversationIDs failed", err)
2 years ago
return err
2 years ago
}
2 years ago
for _, conversationID := range conversationIDs {
conversationIDs = append(conversationIDs, utils.GetNotificationConversationIDByConversationID(conversationID))
2 years ago
}
2 years ago
for _, conversationID := range conversationIDs {
if err := c.fixAndCheckSeq(ctx, conversationID); err != nil {
log.ZError(ctx, "fixSeq failed", err, "conversationID", conversationID)
2 years ago
}
}
fmt.Println("fix all seq finished")
2 years ago
return nil
2 years ago
}