mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-04 09:09:02 +08:00
test cron
This commit is contained in:
parent
2433e27eb5
commit
9025d8bd90
34
internal/cron_task/clear_msg_test.go
Normal file
34
internal/cron_task/clear_msg_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package cronTask
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/constant"
|
||||||
|
"Open_IM/pkg/common/log"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) {
|
||||||
|
operationID := getCronTaskOperationID()
|
||||||
|
testUserIDList := []string{"test_del_id1", "test_del_id2", "test_del_id3", "test_del_id4", "test_del_id5"}
|
||||||
|
for _, userID := range testUserIDList {
|
||||||
|
operationID = userID + "-" + operationID
|
||||||
|
if err := DeleteMongoMsgAndResetRedisSeq(operationID, userID); err != nil {
|
||||||
|
t.Error("checkMaxSeqWithMongo failed", userID)
|
||||||
|
}
|
||||||
|
if err := checkMaxSeqWithMongo(operationID, userID, constant.WriteDiffusion); err != nil {
|
||||||
|
t.Error("checkMaxSeqWithMongo failed", userID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
testWorkingGroupIDList := []string{"test_del_id1", "test_del_id2", "test_del_id3", "test_del_id4", "test_del_id5"}
|
||||||
|
for _, groupID := range testWorkingGroupIDList {
|
||||||
|
operationID = groupID + "-" + operationID
|
||||||
|
log.NewDebug(operationID, utils.GetSelfFuncName(), "groupID:", groupID, "userIDList:", testUserIDList)
|
||||||
|
if err := ResetUserGroupMinSeq(operationID, groupID, testUserIDList); err != nil {
|
||||||
|
t.Error("checkMaxSeqWithMongo failed", groupID)
|
||||||
|
}
|
||||||
|
if err := checkMaxSeqWithMongo(operationID, groupID, constant.ReadDiffusion); err != nil {
|
||||||
|
t.Error("checkMaxSeqWithMongo failed", groupID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ func StartCronTask() {
|
|||||||
c := cron.New()
|
c := cron.New()
|
||||||
fmt.Println("config", config.Config.Mongo.ChatRecordsClearTime)
|
fmt.Println("config", config.Config.Mongo.ChatRecordsClearTime)
|
||||||
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, func() {
|
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, func() {
|
||||||
|
// user msg clear
|
||||||
operationID := getCronTaskOperationID()
|
operationID := getCronTaskOperationID()
|
||||||
log.NewInfo(operationID, "====================== start del cron task ======================")
|
log.NewInfo(operationID, "====================== start del cron task ======================")
|
||||||
userIDList, err := im_mysql_model.SelectAllUserID()
|
userIDList, err := im_mysql_model.SelectAllUserID()
|
||||||
@ -37,8 +38,10 @@ func StartCronTask() {
|
|||||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// working group msg clear
|
||||||
workingGroupIDList, err := im_mysql_model.GetGroupIDListByGroupType(constant.WorkingGroup)
|
workingGroupIDList, err := im_mysql_model.GetGroupIDListByGroupType(constant.WorkingGroup)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
log.NewDebug(operationID, utils.GetSelfFuncName(), "workingGroupIDList: ", workingGroupIDList)
|
||||||
for _, groupID := range workingGroupIDList {
|
for _, groupID := range workingGroupIDList {
|
||||||
userIDList, err = rocksCache.GetGroupMemberIDListFromCache(groupID)
|
userIDList, err = rocksCache.GetGroupMemberIDListFromCache(groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -55,20 +58,19 @@ func StartCronTask() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.NewInfo(operationID, "====================== start del cron finished ======================")
|
log.NewInfo(operationID, "====================== start del cron finished ======================")
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("start cron failed", err.Error())
|
fmt.Println("start cron failed", err.Error(), config.Config.Mongo.ChatRecordsClearTime)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Start()
|
c.Start()
|
||||||
fmt.Println("start cron task success")
|
fmt.Println("start cron task success")
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,17 +264,17 @@ func (d *DataBases) GetUserMsgListByIndex(ID string, index int64) (*UserChat, er
|
|||||||
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
|
||||||
regex := fmt.Sprintf("^%s", ID)
|
regex := fmt.Sprintf("^%s", ID)
|
||||||
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
|
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
|
||||||
var msgs []UserChat
|
var msgs []*UserChat
|
||||||
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
err = cursor.Decode(&msgs)
|
err = cursor.Decode(&msgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
if len(msgs) > 0 {
|
if len(msgs) > 0 {
|
||||||
return &msgs[0], err
|
return msgs[0], err
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("get msg list failed")
|
return nil, errors.New("get msg list failed")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user