diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 3c43e3e20..078eee136 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -7,6 +7,7 @@ import ( "Open_IM/pkg/common/log" server_api_params "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" + goRedis "github.com/go-redis/redis/v8" "github.com/golang/protobuf/proto" "math" ) @@ -21,10 +22,13 @@ func ResetUserGroupMinSeq(operationID, groupID string, userIDList []string) erro log.NewError(operationID, utils.GetSelfFuncName(), groupID, "deleteMongoMsg failed") return utils.Wrap(err, "") } + if minSeq == 0 { + return nil + } log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDList:", delMsgIDList, "minSeq", minSeq) for _, userID := range userIDList { userMinSeq, err := db.DB.GetGroupUserMinSeq(groupID, userID) - if err != nil { + if err != nil && err != goRedis.Nil { log.NewError(operationID, utils.GetSelfFuncName(), "GetGroupUserMinSeq failed", groupID, userID, err.Error()) continue } @@ -46,6 +50,9 @@ func DeleteMongoMsgAndResetRedisSeq(operationID, userID string) error { if err != nil { return utils.Wrap(err, "") } + if minSeq == 0 { + return nil + } log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDMap: ", delMsgIDList, "minSeq", minSeq) err = db.DB.SetUserMinSeq(userID, minSeq) return err @@ -136,6 +143,9 @@ func checkMaxSeqWithMongo(operationID, ID string, diffusionType int) error { maxSeq, err = db.DB.GetGroupMaxSeq(ID) } if err != nil { + if err == goRedis.Nil { + return nil + } return utils.Wrap(err, "GetUserMaxSeq failed") } msg, err := db.DB.GetNewestMsg(ID) diff --git a/internal/cron_task/cron_task.go b/internal/cron_task/cron_task.go index 0b1784b20..a7f90cf91 100644 --- a/internal/cron_task/cron_task.go +++ b/internal/cron_task/cron_task.go @@ -20,7 +20,7 @@ func StartCronTask() { fmt.Println("config", config.Config.Mongo.ChatRecordsClearTime) _, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, func() { operationID := getCronTaskOperationID() - log.NewInfo(operationID, "start", utils.GetSelfFuncName()) + log.NewInfo(operationID, "====================== start del cron task ======================") userIDList, err := im_mysql_model.SelectAllUserID() if err == nil { log.NewDebug(operationID, utils.GetSelfFuncName(), "userIDList: ", userIDList) @@ -56,6 +56,8 @@ func StartCronTask() { log.NewError(operationID, utils.GetSelfFuncName(), err.Error()) return } + + log.NewInfo(operationID, "====================== start del cron finished ======================") }) if err != nil { fmt.Println("start cron failed", err.Error()) diff --git a/pkg/common/db/mongoModel.go b/pkg/common/db/mongoModel.go index 47b556b36..4953aaabc 100644 --- a/pkg/common/db/mongoModel.go +++ b/pkg/common/db/mongoModel.go @@ -263,13 +263,16 @@ func (d *DataBases) GetUserMsgListByIndex(ID string, index int64) (*UserChat, er ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat) regex := fmt.Sprintf("^%s", ID) - findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"$regex": regex}).SetSort(bson.M{"uid": 1}) + findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1}) var msgs []UserChat cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts) if err != nil { return nil, err } err = cursor.Decode(&msgs) + if err != nil { + return nil, utils.Wrap(err, "") + } if len(msgs) > 0 { return &msgs[0], err } else { @@ -306,13 +309,16 @@ func (d *DataBases) GetNewestMsg(ID string) (msg *MsgInfo, err error) { ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat) regex := fmt.Sprintf("^%s", ID) - findOpts := options.Find().SetLimit(1).SetSort(bson.M{"$regex": regex}).SetSort(bson.M{"uid": -1}) + findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": -1}) var userChats []UserChat cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts) if err != nil { return nil, err } err = cursor.Decode(&userChats) + if err != nil { + return nil, utils.Wrap(err, "") + } if len(userChats) > 0 { if len(userChats[0].Msg) > 0 { return &userChats[0].Msg[len(userChats[0].Msg)], nil