diff --git a/config/config.yaml b/config/config.yaml index 61aadf2b3..7980eaa6f 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -36,7 +36,7 @@ mongo: dbPassword: #mongo密码,建议先不设置 dbMaxPoolSize: 100 dbRetainChatRecords: 3650 #mongo保存离线消息时间(天),根据需求修改 - chatRecordsClearTime: "* * * * *" # 每天凌晨3点清除消息,该配置和linux定时任务一样, 清理操作建议设置在用户活跃少的时候 # 0 3 * * * + chatRecordsClearTime: "0 3 * * *" # 每天凌晨3点清除消息,该配置和linux定时任务一样, 清理操作建议设置在用户活跃少的时候 # 0 3 * * * redis: dbAddress: [ 127.0.0.1:16379 ] #redis地址 单机时,填写一个地址即可,使用redis集群时候,填写集群中多个节点地址(主从地址都可以填写,增加容灾能力),默认即可 diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 5ae3b16c6..08ef74cef 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -119,6 +119,9 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs } else { var hasMarkDelFlag bool for index, msg := range msgs.Msg { + if msg.SendTime == 0 { + continue + } msgPb := &server_api_params.MsgData{} err = proto.Unmarshal(msg.Msg, msgPb) if err != nil { diff --git a/internal/cron_task/clear_msg_test.go b/internal/cron_task/clear_msg_test.go index 627ba1431..465216ce9 100644 --- a/internal/cron_task/clear_msg_test.go +++ b/internal/cron_task/clear_msg_test.go @@ -13,6 +13,7 @@ import ( "github.com/golang/protobuf/proto" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + "gopkg.in/mgo.v2/bson" "testing" "time" @@ -71,6 +72,11 @@ func CreateChat(userChat *db.UserChat) error { return err } +func DelChat(uid string, index int) error { + _, err := mongoClient.DeleteOne(context.Background(), bson.M{"uid": uid + ":" + strconv.Itoa(index)}) + return err +} + func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { operationID := getCronTaskOperationID() redisClient = redis.NewClient(&redis.Options{ @@ -84,10 +90,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri)) mongoClient = client.Database("openIM").Collection("msg") testUID1 := "test_del_id1" - - //testUID4 := "test_del_id4" - //testUID5 := "test_del_id5" - //testUID6 := "test_del_id6" + err = DelChat(testUID1, 0) err = SetUserMaxSeq(testUID1, 600) userChat := GenUserChat(1, 600, 200, 0, testUID1) err = CreateChat(userChat) @@ -106,11 +109,14 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { } testUID2 := "test_del_id2" + err = DelChat(testUID2, 0) + err = DelChat(testUID2, 1) err = SetUserMaxSeq(testUID2, 7000) userChat = GenUserChat(1, 4999, 5000, 0, testUID2) userChat2 := GenUserChat(5000, 7000, 6000, 1, testUID2) err = CreateChat(userChat) err = CreateChat(userChat2) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID2); err != nil { t.Error("checkMaxSeqWithMongo failed", testUID2) } @@ -126,6 +132,7 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { } testUID3 := "test_del_id3" + err = DelChat(testUID3, 0) err = SetUserMaxSeq(testUID3, 4999) userChat = GenUserChat(1, 4999, 5000, 0, testUID3) err = CreateChat(userChat) @@ -144,6 +151,9 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { } testUID4 := "test_del_id4" + err = DelChat(testUID4, 0) + err = DelChat(testUID4, 1) + err = DelChat(testUID4, 2) err = SetUserMaxSeq(testUID4, 12000) userChat = GenUserChat(1, 4999, 5000, 0, testUID4) userChat2 = GenUserChat(5000, 9999, 10000, 1, testUID4) @@ -162,6 +172,55 @@ func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { t.Error("err is not nil", testUID4, err.Error()) } if minSeq != 11001 { - t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 11001) + t.Error("test4 is not the same", "minSeq:", minSeq, "targetSeq", 11001) + } + + testUID5 := "test_del_id5" + err = DelChat(testUID5, 0) + err = DelChat(testUID5, 1) + err = SetUserMaxSeq(testUID5, 9999) + userChat = GenUserChat(1, 4999, 5000, 0, testUID5) + userChat2 = GenUserChat(5000, 9999, 10000, 1, testUID5) + err = CreateChat(userChat) + err = CreateChat(userChat2) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID5); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID4) + } + if err := checkMaxSeqWithMongo(operationID, testUID5, constant.WriteDiffusion); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID5) + } + minSeq, err = GetUserMinSeq(testUID5) + if err != nil { + t.Error("err is not nil", testUID5, err.Error()) + } + if minSeq != 10000 { + t.Error("test5 is not the same", "minSeq:", minSeq, "targetSeq", 10000) + } + + testUID6 := "test_del_id6" + err = DelChat(testUID5, 0) + err = DelChat(testUID5, 1) + err = DelChat(testUID5, 2) + err = DelChat(testUID5, 3) + userChat = GenUserChat(1, 4999, 5000, 0, testUID6) + userChat2 = GenUserChat(5000, 9999, 10000, 1, testUID6) + userChat3 = GenUserChat(10000, 14999, 13000, 2, testUID6) + userChat4 := GenUserChat(15000, 19999, 0, 3, testUID6) + err = CreateChat(userChat) + err = CreateChat(userChat2) + err = CreateChat(userChat3) + err = CreateChat(userChat4) + if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID6); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID6) + } + if err := checkMaxSeqWithMongo(operationID, testUID6, constant.WriteDiffusion); err != nil { + t.Error("checkMaxSeqWithMongo failed", testUID6) + } + minSeq, err = GetUserMinSeq(testUID6) + if err != nil { + t.Error("err is not nil", testUID6, err.Error()) + } + if minSeq != 13001 { + t.Error("test3 is not the same", "minSeq:", minSeq, "targetSeq", 13001) } } diff --git a/script/check_all.sh b/script/check_all.sh index 76535c79f..af3d44ceb 100644 --- a/script/check_all.sh +++ b/script/check_all.sh @@ -54,13 +54,13 @@ else fi -#check=$(ps aux | grep -w ./${cron_task_name} | grep -v grep | wc -l) -#if [ $check -ge 1 ]; then -# echo -e ${GREEN_PREFIX}"none port has been listening,belongs service is openImCronTask"${COLOR_SUFFIX} -#else -# echo -e ${RED_PREFIX}"cron_task_name service does not start normally"${COLOR_SUFFIX} -# echo -e ${RED_PREFIX}"please check ../logs/openIM.log "${COLOR_SUFFIX} -# exit -1 -#fi -# -#echo -e ${YELLOW_PREFIX}"all services launch success"${COLOR_SUFFIX} +check=$(ps aux | grep -w ./${cron_task_name} | grep -v grep | wc -l) +if [ $check -ge 1 ]; then + echo -e ${GREEN_PREFIX}"none port has been listening,belongs service is openImCronTask"${COLOR_SUFFIX} +else + echo -e ${RED_PREFIX}"cron_task_name service does not start normally"${COLOR_SUFFIX} + echo -e ${RED_PREFIX}"please check ../logs/openIM.log "${COLOR_SUFFIX} + exit -1 +fi + +echo -e ${YELLOW_PREFIX}"all services launch success"${COLOR_SUFFIX} diff --git a/script/start_all.sh b/script/start_all.sh index 223187973..4a5f7d65e 100644 --- a/script/start_all.sh +++ b/script/start_all.sh @@ -10,7 +10,7 @@ need_to_start_server_shell=( sdk_svr_start.sh msg_gateway_start.sh demo_svr_start.sh -# start_cron.sh + start_cron.sh ) time=`date +"%Y-%m-%d %H:%M:%S"` echo "==========================================================">>../logs/openIM.log 2>&1 &