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/crontask/clear_msg.go

104 lines
3.6 KiB

2 years ago
package cronTask
2 years ago
import (
"Open_IM/pkg/common/config"
2 years ago
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/controller"
2 years ago
"Open_IM/pkg/common/log"
2 years ago
"Open_IM/pkg/common/tracelog"
2 years ago
"Open_IM/pkg/utils"
2 years ago
"context"
2 years ago
"math"
2 years ago
)
2 years ago
2 years ago
type ClearMsgTool struct {
msgInterface controller.MsgInterface
userInterface controller.UserInterface
groupInterface controller.GroupInterface
2 years ago
}
2 years ago
func (c *ClearMsgTool) getCronTaskOperationID() string {
2 years ago
return cronTaskOperationID + utils.OperationIDGenerator()
2 years ago
}
2 years ago
func (c *ClearMsgTool) ClearAll() {
2 years ago
operationID := c.getCronTaskOperationID()
ctx := context.Background()
tracelog.SetOperationID(ctx, operationID)
2 years ago
log.NewInfo(operationID, "============================ start del cron task ============================")
2 years ago
var err error
2 years ago
userIDList, err := c.userInterface.GetAllUserID(ctx)
2 years ago
if err == nil {
2 years ago
c.ClearUsersMsg(ctx, userIDList)
2 years ago
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
// working group msg clear
2 years ago
workingGroupIDList, err := c.groupInterface.GetGroupIDsByGroupType(ctx, constant.WorkingGroup)
2 years ago
if err == nil {
2 years ago
c.ClearSuperGroupMsg(ctx, workingGroupIDList)
2 years ago
} else {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
}
2 years ago
log.NewInfo(operationID, "============================ start del cron finished ============================")
2 years ago
}
2 years ago
func (c *ClearMsgTool) ClearUsersMsg(ctx context.Context, userIDList []string) {
2 years ago
for _, userID := range userIDList {
2 years ago
if err := c.msgInterface.DeleteUserMsgsAndSetMinSeq(ctx, userID, int64(config.Config.Mongo.DBRetainChatRecords * 24 *60 *60)); err != nil {
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), userID)
2 years ago
}
2 years ago
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err := c.msgInterface.GetUserMinMaxSeqInMongoAndCache(ctx, userID)
if err != nil {
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), err.Error(), "GetUserMinMaxSeqInMongoAndCache failed", userID)
continue
2 years ago
}
2 years ago
if
2 years ago
}
2 years ago
}
2 years ago
func (c *ClearMsgTool) ClearSuperGroupMsg(ctx context.Context, workingGroupIDList []string) {
2 years ago
for _, groupID := range workingGroupIDList {
2 years ago
userIDs, err := c.groupInterface.FindGroupMemberUserID(ctx, groupID)
2 years ago
if err != nil {
2 years ago
log.NewError(tracelog.GetOperationID(ctx), utils.GetSelfFuncName(), "FindGroupMemberUserID", err.Error(), groupID)
2 years ago
continue
2 years ago
}
2 years ago
if err := c.msgInterface.DeleteUserSuperGroupMsgsAndSetMinSeq(ctx, groupID, userIDs, int64(config.Config.Mongo.DBRetainChatRecords * 24 *60 *60)); err != nil {
//log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userIDList)
2 years ago
}
2 years ago
minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err := c.msgInterface.GetSuperGroupMinMaxSeqInMongoAndCache(ctx, groupID)
2 years ago
}
}
2 years ago
func (c *ClearMsgTool) checkMaxSeqWithMongo(ctx context.Context, sourceID string, diffusionType int) error {
2 years ago
var seqRedis uint64
2 years ago
var err error
if diffusionType == constant.WriteDiffusion {
2 years ago
seqRedis, err = db.DB.GetUserMaxSeq(sourceID)
2 years ago
} else {
2 years ago
seqRedis, err = db.DB.GetGroupMaxSeq(sourceID)
2 years ago
}
if err != nil {
2 years ago
if err == goRedis.Nil {
return nil
}
2 years ago
return utils.Wrap(err, "GetUserMaxSeq failed")
}
2 years ago
msg, err := db.DB.GetNewestMsg(sourceID)
2 years ago
if err != nil {
return utils.Wrap(err, "GetNewestMsg failed")
}
2 years ago
if msg == nil {
return nil
}
if math.Abs(float64(msg.Seq-uint32(seqRedis))) > 10 {
2 years ago
log.NewWarn(operationID, utils.GetSelfFuncName(), "seqMongo, seqRedis", msg.Seq, seqRedis, sourceID, "redis maxSeq is different with msg.Seq > 10", "status: ", msg.Status, msg.SendTime)
2 years ago
} else {
2 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), "seqMongo, seqRedis", msg.Seq, seqRedis, sourceID, "seq and msg OK", "status:", msg.Status, msg.SendTime)
2 years ago
}
return nil
}