parent
20dfafd218
commit
dc57d38856
@ -0,0 +1,77 @@
|
||||
package msg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||||
"github.com/openimsdk/protocol/conversation"
|
||||
"github.com/openimsdk/protocol/msg"
|
||||
"github.com/openimsdk/protocol/wrapperspb"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *msgServer) ClearMsg(ctx context.Context, req *msg.ClearMsgReq) (_ *msg.ClearMsgResp, err error) {
|
||||
if err := authverify.CheckAdmin(ctx, m.config.Share.IMAdminUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.Timestamp > time.Now().UnixMilli() {
|
||||
return nil, errs.ErrArgs.WrapMsg("request millisecond timestamp error")
|
||||
}
|
||||
var (
|
||||
docNum int
|
||||
msgNum int
|
||||
start = time.Now()
|
||||
)
|
||||
clearMsg := func(ctx context.Context) (bool, error) {
|
||||
conversationSeqs := make(map[string]struct{})
|
||||
defer func() {
|
||||
req := &conversation.UpdateConversationReq{
|
||||
MsgDestructTime: wrapperspb.Int64(time.Now().UnixMilli()),
|
||||
}
|
||||
for conversationID := range conversationSeqs {
|
||||
req.ConversationID = conversationID
|
||||
if err := m.Conversation.UpdateConversations(ctx, req); err != nil {
|
||||
log.ZError(ctx, "update conversation max seq failed", err, "conversationID", conversationID, "msgDestructTime", req.MsgDestructTime)
|
||||
}
|
||||
}
|
||||
}()
|
||||
msgs, err := m.MsgDatabase.GetBeforeMsg(ctx, req.Timestamp, 100)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(msgs) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
index, err := m.MsgDatabase.DeleteDocMsgBefore(ctx, req.Timestamp, msg)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(index) == 0 {
|
||||
return false, errs.ErrInternalServer.WrapMsg("delete doc msg failed")
|
||||
}
|
||||
docNum++
|
||||
msgNum += len(index)
|
||||
conversationID := msg.DocID[:strings.LastIndex(msg.DocID, ":")]
|
||||
if _, ok := conversationSeqs[conversationID]; !ok {
|
||||
conversationSeqs[conversationID] = struct{}{}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
for {
|
||||
keep, err := clearMsg(ctx)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "clear msg failed", err, "docNum", docNum, "msgNum", msgNum, "cost", time.Since(start))
|
||||
return nil, err
|
||||
}
|
||||
if !keep {
|
||||
log.ZInfo(ctx, "clear msg success", "docNum", docNum, "msgNum", msgNum, "cost", time.Since(start))
|
||||
break
|
||||
}
|
||||
log.ZInfo(ctx, "clearing message", "docNum", docNum, "msgNum", msgNum, "cost", time.Since(start))
|
||||
}
|
||||
return &msg.ClearMsgResp{}, nil
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||
"github.com/openimsdk/protocol/sdkws"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/mcontext"
|
||||
"github.com/openimsdk/tools/utils/idutil"
|
||||
"github.com/openimsdk/tools/utils/stringutil"
|
||||
)
|
||||
|
||||
// func (c *MsgTool) ConversationsDestructMsgs() {
|
||||
// log.ZInfo(context.Background(), "start msg destruct cron task")
|
||||
// ctx := mcontext.NewCtx(utils.GetSelfFuncName())
|
||||
// conversations, err := c.conversationDatabase.GetConversationIDsNeedDestruct(ctx)
|
||||
// if err != nil {
|
||||
// log.ZError(ctx, "get conversation id need destruct failed", err)
|
||||
// return
|
||||
// }
|
||||
// log.ZDebug(context.Background(), "nums conversations need destruct", "nums", len(conversations))
|
||||
// for _, conversation := range conversations {
|
||||
// ctx = mcontext.NewCtx(utils.GetSelfFuncName() + "-" + utils.OperationIDGenerator() + "-" + conversation.ConversationID + "-" + conversation.OwnerUserID)
|
||||
// log.ZDebug(
|
||||
// ctx,
|
||||
// "UserMsgsDestruct",
|
||||
// "conversationID",
|
||||
// conversation.ConversationID,
|
||||
// "ownerUserID",
|
||||
// conversation.OwnerUserID,
|
||||
// "msgDestructTime",
|
||||
// conversation.MsgDestructTime,
|
||||
// "lastMsgDestructTime",
|
||||
// conversation.LatestMsgDestructTime,
|
||||
// )
|
||||
// now := time.Now()
|
||||
// seqs, err := c.msgDatabase.UserMsgsDestruct(ctx, conversation.OwnerUserID, conversation.ConversationID, conversation.MsgDestructTime, conversation.LatestMsgDestructTime)
|
||||
// if err != nil {
|
||||
// log.ZError(ctx, "user msg destruct failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
||||
// continue
|
||||
// }
|
||||
// if len(seqs) > 0 {
|
||||
// if err := c.conversationDatabase.UpdateUsersConversationField(ctx, []string{conversation.OwnerUserID}, conversation.ConversationID, map[string]interface{}{"latest_msg_destruct_time": now}); err
|
||||
// != nil {
|
||||
// log.ZError(ctx, "updateUsersConversationField failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
||||
// continue
|
||||
// }
|
||||
// if err := c.msgNotificationSender.UserDeleteMsgsNotification(ctx, conversation.OwnerUserID, conversation.ConversationID, seqs); err != nil {
|
||||
// log.ZError(ctx, "userDeleteMsgsNotification failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
func (c *MsgTool) ConversationsDestructMsgs() {
|
||||
log.ZInfo(context.Background(), "start msg destruct cron task")
|
||||
ctx := mcontext.NewCtx(stringutil.GetSelfFuncName())
|
||||
num, err := c.conversationDatabase.GetAllConversationIDsNumber(ctx)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "GetAllConversationIDsNumber failed", err)
|
||||
return
|
||||
}
|
||||
const batchNum = 50
|
||||
log.ZDebug(ctx, "GetAllConversationIDsNumber", "num", num)
|
||||
if num == 0 {
|
||||
return
|
||||
}
|
||||
count := int(num/batchNum + num/batchNum/2)
|
||||
if count < 1 {
|
||||
count = 1
|
||||
}
|
||||
maxPage := 1 + num/batchNum
|
||||
if num%batchNum != 0 {
|
||||
maxPage++
|
||||
}
|
||||
for i := 0; i < count; i++ {
|
||||
pageNumber := rand.Int63() % maxPage
|
||||
pagination := &sdkws.RequestPagination{
|
||||
PageNumber: int32(pageNumber),
|
||||
ShowNumber: batchNum,
|
||||
}
|
||||
conversationIDs, err := c.conversationDatabase.PageConversationIDs(ctx, pagination)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber)
|
||||
continue
|
||||
}
|
||||
log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber, "conversationIDsNum", len(conversationIDs), "conversationIDs", conversationIDs)
|
||||
if len(conversationIDs) == 0 {
|
||||
continue
|
||||
}
|
||||
conversations, err := c.conversationDatabase.GetConversationsByConversationID(ctx, conversationIDs)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "GetConversationsByConversationID failed", err, "conversationIDs", conversationIDs)
|
||||
continue
|
||||
}
|
||||
temp := make([]*relation.ConversationModel, 0, len(conversations))
|
||||
for i, conversation := range conversations {
|
||||
if conversation.IsMsgDestruct && conversation.MsgDestructTime != 0 && (time.Now().Unix() > (conversation.MsgDestructTime+conversation.LatestMsgDestructTime.Unix()+8*60*60)) ||
|
||||
conversation.LatestMsgDestructTime.IsZero() {
|
||||
temp = append(temp, conversations[i])
|
||||
}
|
||||
}
|
||||
for _, conversation := range temp {
|
||||
ctx = mcontext.NewCtx(stringutil.GetSelfFuncName() + "-" + idutil.OperationIDGenerator() + "-" + conversation.ConversationID + "-" + conversation.OwnerUserID)
|
||||
log.ZDebug(
|
||||
ctx,
|
||||
"UserMsgsDestruct",
|
||||
"conversationID",
|
||||
conversation.ConversationID,
|
||||
"ownerUserID",
|
||||
conversation.OwnerUserID,
|
||||
"msgDestructTime",
|
||||
conversation.MsgDestructTime,
|
||||
"lastMsgDestructTime",
|
||||
conversation.LatestMsgDestructTime,
|
||||
)
|
||||
now := time.Now()
|
||||
seqs, err := c.msgDatabase.UserMsgsDestruct(ctx, conversation.OwnerUserID, conversation.ConversationID, conversation.MsgDestructTime, conversation.LatestMsgDestructTime)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "user msg destruct failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
||||
continue
|
||||
}
|
||||
if len(seqs) > 0 {
|
||||
if err := c.conversationDatabase.UpdateUsersConversationField(ctx, []string{conversation.OwnerUserID}, conversation.ConversationID, map[string]any{"latest_msg_destruct_time": now}); err != nil {
|
||||
log.ZError(ctx, "updateUsersConversationField failed", err, "conversationID", conversation.ConversationID, "ownerUserID", conversation.OwnerUserID)
|
||||
continue
|
||||
}
|
||||
c.msgNotificationSender.UserDeleteMsgsNotification(ctx, conversation.OwnerUserID, conversation.ConversationID, seqs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,113 +0,0 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDisLock(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{})
|
||||
defer rdb.Close()
|
||||
|
||||
assert.Equal(t, true, netlock(rdb, "cron-1", 1*time.Second))
|
||||
|
||||
// if exists, get false
|
||||
assert.Equal(t, false, netlock(rdb, "cron-1", 1*time.Second))
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// wait for key on timeout, get true
|
||||
assert.Equal(t, true, netlock(rdb, "cron-1", 2*time.Second))
|
||||
|
||||
// set different key
|
||||
assert.Equal(t, true, netlock(rdb, "cron-2", 2*time.Second))
|
||||
}
|
||||
|
||||
//func TestCronWrapFunc(t *testing.T) {
|
||||
// rdb := redis.NewClient(&redis.Options{})
|
||||
// defer rdb.Close()
|
||||
//
|
||||
// once := sync.Once{}
|
||||
// done := make(chan struct{}, 1)
|
||||
// cb := func() {
|
||||
// once.Do(func() {
|
||||
// close(done)
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// start := time.Now()
|
||||
// key := fmt.Sprintf("cron-%v", rand.Int31())
|
||||
// crontab := cron.New(cron.WithSeconds())
|
||||
// crontab.AddFunc("*/1 * * * * *", cronWrapFunc(config.NewGlobalConfig(), rdb, key, cb))
|
||||
// crontab.Start()
|
||||
// <-done
|
||||
//
|
||||
// dur := time.Since(start)
|
||||
// assert.LessOrEqual(t, dur.Seconds(), float64(2*time.Second))
|
||||
// crontab.Stop()
|
||||
//}
|
||||
//
|
||||
//func TestCronWrapFuncWithNetlock(t *testing.T) {
|
||||
// conf, err := initCfg()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// conf.EnableCronLocker = true
|
||||
// rdb := redis.NewClient(&redis.Options{})
|
||||
// defer rdb.Close()
|
||||
//
|
||||
// done := make(chan string, 10)
|
||||
//
|
||||
// crontab := cron.New(cron.WithSeconds())
|
||||
//
|
||||
// key := fmt.Sprintf("cron-%v", rand.Int31())
|
||||
// crontab.AddFunc("*/1 * * * * *", cronWrapFunc(conf, rdb, key, func() {
|
||||
// done <- "host1"
|
||||
// }))
|
||||
// crontab.AddFunc("*/1 * * * * *", cronWrapFunc(conf, rdb, key, func() {
|
||||
// done <- "host2"
|
||||
// }))
|
||||
// crontab.Start()
|
||||
//
|
||||
// time.Sleep(12 * time.Second)
|
||||
// // the ttl of netlock is 5s, so expected value is 2.
|
||||
// assert.Equal(t, len(done), 2)
|
||||
//
|
||||
// crontab.Stop()
|
||||
//}
|
||||
//
|
||||
//func initCfg() (*config.GlobalConfig, error) {
|
||||
// const (
|
||||
// defaultCfgPath = "../../../../../config/config.yaml"
|
||||
// )
|
||||
//
|
||||
// cfgPath := flag.String("c", defaultCfgPath, "Path to the configuration file")
|
||||
// data, err := os.ReadFile(*cfgPath)
|
||||
// if err != nil {
|
||||
// return nil, errs.WrapMsg(err, "ReadFile unmarshal failed")
|
||||
// }
|
||||
//
|
||||
// conf := config.NewGlobalConfig()
|
||||
// err = yaml.Unmarshal(data, &conf)
|
||||
// if err != nil {
|
||||
// return nil, errs.WrapMsg(err, "InitConfig unmarshal failed")
|
||||
// }
|
||||
// return conf, nil
|
||||
//}
|
@ -1,226 +0,0 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/openimsdk/open-im-server/v3/internal/rpc/msg"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/controller"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/util/conversationutil"
|
||||
"github.com/openimsdk/protocol/sdkws"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/mcontext"
|
||||
"github.com/openimsdk/tools/utils/stringutil"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type MsgTool struct {
|
||||
msgDatabase controller.CommonMsgDatabase
|
||||
conversationDatabase controller.ConversationDatabase
|
||||
userDatabase controller.UserDatabase
|
||||
groupDatabase controller.GroupDatabase
|
||||
msgNotificationSender *msg.MsgNotificationSender
|
||||
config *CronTaskConfig
|
||||
}
|
||||
|
||||
func NewMsgTool(msgDatabase controller.CommonMsgDatabase, userDatabase controller.UserDatabase,
|
||||
groupDatabase controller.GroupDatabase, conversationDatabase controller.ConversationDatabase,
|
||||
msgNotificationSender *msg.MsgNotificationSender, config *CronTaskConfig,
|
||||
) *MsgTool {
|
||||
return &MsgTool{
|
||||
msgDatabase: msgDatabase,
|
||||
userDatabase: userDatabase,
|
||||
groupDatabase: groupDatabase,
|
||||
conversationDatabase: conversationDatabase,
|
||||
msgNotificationSender: msgNotificationSender,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func InitMsgTool(ctx context.Context, config *CronTaskConfig) (*MsgTool, error) {
|
||||
ch := make(chan int)
|
||||
<-ch
|
||||
//mgocli, err := mongoutil.NewMongoDB(ctx, config.MongodbConfig.Build())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//rdb, err := redisutil.NewRedisClient(ctx, config.RedisConfig.Build())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//discov, err := kdisc.NewDiscoveryRegister(&config.ZookeeperConfig, &config.Share)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//discov.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, "round_robin")))
|
||||
//userDB, err := mgo.NewUserMongo(mgocli.GetDB())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
////msgDatabase, err := controller.InitCommonMsgDatabase(rdb, mgocli.GetDB(), config)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//userMongoDB := mgo.NewUserMongoDriver(mgocli.GetDB())
|
||||
//userDatabase := controller.NewUserDatabase(
|
||||
// userDB,
|
||||
// cache.NewUserCacheRedis(rdb, userDB, cache.GetDefaultOpt()),
|
||||
// mgocli.GetTx(),
|
||||
// userMongoDB,
|
||||
//)
|
||||
//groupDB, err := mgo.NewGroupMongo(mgocli.GetDB())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//groupMemberDB, err := mgo.NewGroupMember(mgocli.GetDB())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//groupRequestDB, err := mgo.NewGroupRequestMgo(mgocli.GetDB())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//conversationDB, err := mgo.NewConversationMongo(mgocli.GetDB())
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//groupDatabase := controller.NewGroupDatabase(rdb, groupDB, groupMemberDB, groupRequestDB, mgocli.GetTx(), nil)
|
||||
//conversationDatabase := controller.NewConversationDatabase(
|
||||
// conversationDB,
|
||||
// cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB),
|
||||
// mgocli.GetTx(),
|
||||
//)
|
||||
//msgRpcClient := rpcclient.NewMessageRpcClient(discov, config.Share.RpcRegisterName.Msg)
|
||||
//msgNotificationSender := notification.NewMsgNotificationSender(config, rpcclient.WithRpcClient(&msgRpcClient))
|
||||
//msgTool := NewMsgTool(msgDatabase, userDatabase, groupDatabase, conversationDatabase, msgNotificationSender, config)
|
||||
//return msgTool, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// func (c *MsgTool) AllConversationClearMsgAndFixSeq() {
|
||||
// ctx := mcontext.NewCtx(utils.GetSelfFuncName())
|
||||
// log.ZInfo(ctx, "============================ start del cron task ============================")
|
||||
// conversationIDs, err := c.conversationDatabase.GetAllConversationIDs(ctx)
|
||||
// if err != nil {
|
||||
// log.ZError(ctx, "GetAllConversationIDs failed", err)
|
||||
// return
|
||||
// }
|
||||
// for _, conversationID := range conversationIDs {
|
||||
// conversationIDs = append(conversationIDs, utils.GetNotificationConversationIDByConversationID(conversationID))
|
||||
// }
|
||||
// c.ClearConversationsMsg(ctx, conversationIDs)
|
||||
// log.ZInfo(ctx, "============================ start del cron finished ============================")
|
||||
//}
|
||||
|
||||
func (c *MsgTool) AllConversationClearMsgAndFixSeq() {
|
||||
ctx := mcontext.NewCtx(stringutil.GetSelfFuncName())
|
||||
log.ZInfo(ctx, "============================ start del cron task ============================")
|
||||
num, err := c.conversationDatabase.GetAllConversationIDsNumber(ctx)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "GetAllConversationIDsNumber failed", err)
|
||||
return
|
||||
}
|
||||
const batchNum = 50
|
||||
log.ZDebug(ctx, "GetAllConversationIDsNumber", "num", num)
|
||||
if num == 0 {
|
||||
return
|
||||
}
|
||||
count := int(num/batchNum + num/batchNum/2)
|
||||
if count < 1 {
|
||||
count = 1
|
||||
}
|
||||
maxPage := 1 + num/batchNum
|
||||
if num%batchNum != 0 {
|
||||
maxPage++
|
||||
}
|
||||
for i := 0; i < count; i++ {
|
||||
pageNumber := rand.Int63() % maxPage
|
||||
pagination := &sdkws.RequestPagination{
|
||||
PageNumber: int32(pageNumber),
|
||||
ShowNumber: batchNum,
|
||||
}
|
||||
conversationIDs, err := c.conversationDatabase.PageConversationIDs(ctx, pagination)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber)
|
||||
continue
|
||||
}
|
||||
log.ZDebug(ctx, "PageConversationIDs failed", "pageNumber", pageNumber, "conversationIDsNum", len(conversationIDs), "conversationIDs", conversationIDs)
|
||||
if len(conversationIDs) == 0 {
|
||||
continue
|
||||
}
|
||||
c.ClearConversationsMsg(ctx, conversationIDs)
|
||||
}
|
||||
log.ZInfo(ctx, "============================ start del cron finished ============================")
|
||||
}
|
||||
|
||||
func (c *MsgTool) ClearConversationsMsg(ctx context.Context, conversationIDs []string) {
|
||||
for _, conversationID := range conversationIDs {
|
||||
if err := c.msgDatabase.DeleteConversationMsgsAndSetMinSeq(ctx, conversationID, int64(c.config.CronTask.RetainChatRecords*24*60*60)); err != nil {
|
||||
log.ZError(ctx, "DeleteUserSuperGroupMsgsAndSetMinSeq failed", err, "conversationID",
|
||||
conversationID, "DBRetainChatRecords", c.config.CronTask.RetainChatRecords)
|
||||
}
|
||||
if err := c.checkMaxSeq(ctx, conversationID); err != nil {
|
||||
log.ZError(ctx, "fixSeq failed", err, "conversationID", conversationID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MsgTool) checkMaxSeqWithMongo(ctx context.Context, conversationID string, maxSeqCache int64) error {
|
||||
minSeqMongo, maxSeqMongo, err := c.msgDatabase.GetMongoMaxAndMinSeq(ctx, conversationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if math.Abs(float64(maxSeqMongo-maxSeqCache)) > 10 {
|
||||
err = fmt.Errorf("cache max seq and mongo max seq is diff > 10, maxSeqMongo:%d,minSeqMongo:%d,maxSeqCache:%d,conversationID:%s", maxSeqMongo, minSeqMongo, maxSeqCache, conversationID)
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MsgTool) checkMaxSeq(ctx context.Context, conversationID string) error {
|
||||
maxSeq, err := c.msgDatabase.GetMaxSeq(ctx, conversationID)
|
||||
if err != nil {
|
||||
if errs.Unwrap(err) == redis.Nil {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err := c.checkMaxSeqWithMongo(ctx, conversationID, maxSeq); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MsgTool) FixAllSeq(ctx context.Context) error {
|
||||
conversationIDs, err := c.conversationDatabase.GetAllConversationIDs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, conversationID := range conversationIDs {
|
||||
conversationIDs = append(conversationIDs, conversationutil.GetNotificationConversationIDByConversationID(conversationID))
|
||||
}
|
||||
for _, conversationID := range conversationIDs {
|
||||
if err := c.checkMaxSeq(ctx, conversationID); err != nil {
|
||||
log.ZWarn(ctx, "fixSeq failed", err, "conversationID", conversationID)
|
||||
}
|
||||
}
|
||||
fmt.Println("fix all seq finished")
|
||||
return nil
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package tools
|
||||
|
||||
import (
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||
"github.com/openimsdk/protocol/constant"
|
||||
"github.com/openimsdk/tools/log"
|
||||
"github.com/openimsdk/tools/mcontext"
|
||||
)
|
||||
|
||||
func (c *MsgTool) convertTools() {
|
||||
ctx := mcontext.NewCtx("convert")
|
||||
conversationIDs, err := c.conversationDatabase.GetAllConversationIDs(ctx)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "get all conversation ids failed", err)
|
||||
return
|
||||
}
|
||||
for _, conversationID := range conversationIDs {
|
||||
conversationIDs = append(conversationIDs, msgprocessor.GetNotificationConversationIDByConversationID(conversationID))
|
||||
}
|
||||
_, userIDs, err := c.userDatabase.GetAllUserID(ctx, nil)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "get all user ids failed", err)
|
||||
return
|
||||
}
|
||||
log.ZDebug(ctx, "all userIDs", "len userIDs", len(userIDs))
|
||||
for _, userID := range userIDs {
|
||||
conversationIDs = append(conversationIDs, msgprocessor.GetConversationIDBySessionType(constant.SingleChatType, userID, userID))
|
||||
conversationIDs = append(conversationIDs, msgprocessor.GetNotificationConversationID(constant.SingleChatType, userID, userID))
|
||||
}
|
||||
log.ZDebug(ctx, "all conversationIDs", "len userIDs", len(conversationIDs))
|
||||
c.msgDatabase.ConvertMsgsDocLen(ctx, conversationIDs)
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package mgo
|
||||
|
||||
import (
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func NewMsgDelMongo(db *mongo.Database) *MsgDelMgo {
|
||||
return &MsgDelMgo{coll: db.Collection(new(relation.MsgDocModel).TableName())}
|
||||
}
|
||||
|
||||
type MsgDelMgo struct {
|
||||
coll *mongo.Collection
|
||||
model relation.MsgDocModel
|
||||
}
|
||||
|
||||
//func (m *MsgDelMgo) getEmptyMsg(ctx context.Context, limit int) ([]string, error) {
|
||||
// return mongoutil.Aggregate[string](ctx, m.coll, []bson.M{
|
||||
// {
|
||||
// "$match": bson.M{
|
||||
// "msgs": bson.M{
|
||||
// "$exists": true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// "$project": bson.M{
|
||||
// "_id": 0,
|
||||
// "doc_id": 1,
|
||||
// "all_null_msgs": bson.M{
|
||||
// "$not": []bson.M{
|
||||
// {
|
||||
// "$anyElementTrue": bson.M{
|
||||
// "$map": bson.M{
|
||||
// "input": "$msgs",
|
||||
// "as": "item",
|
||||
// "in": "$$item.msg",
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// "$project": bson.M{
|
||||
// "doc_id": 1,
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// "$limit": limit,
|
||||
// },
|
||||
// })
|
||||
//}
|
||||
//
|
||||
//func (m *MsgDelMgo) deleteEmptyMsgs(ctx context.Context) error {
|
||||
// for {
|
||||
// docIDs, err := m.getEmptyMsg(ctx, 100)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// if len(docIDs) == 0 {
|
||||
// return nil
|
||||
// }
|
||||
// for _, docID := range docIDs {
|
||||
// if err := m.deleteEmptyMsg(ctx, docID); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
@ -0,0 +1,46 @@
|
||||
package mgo
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
//conf := config.Mongo{
|
||||
// Address: []string{"localhost:37017"},
|
||||
// Username: "openIM",
|
||||
// Password: "openIM123",
|
||||
// Database: "demo",
|
||||
//}
|
||||
//conf.URI = `mongodb://openIM:openIM123@localhost:37017/demo?maxPoolSize=100&authSource=admin`
|
||||
//cli, err := mongoutil.NewMongoDB(context.Background(), conf.Build())
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//msg, _ := NewMsgMongo(cli.GetDB())
|
||||
//count, err := msg.ClearMsg(context.Background(), time.Now().Add(-time.Hour*24*61))
|
||||
//if err != nil {
|
||||
// t.Log("error", err)
|
||||
// return
|
||||
//}
|
||||
//t.Log("count", count)
|
||||
|
||||
s := `si_5300327160_9129042887:0123`
|
||||
|
||||
t.Log(s[:strings.LastIndex(s, ":")])
|
||||
|
||||
}
|
||||
|
||||
func TestName2(t *testing.T) {
|
||||
m := map[string]string{
|
||||
"1": "1",
|
||||
"2": "2",
|
||||
}
|
||||
t.Log(m)
|
||||
clear(m)
|
||||
t.Log(m)
|
||||
a := []string{"1", "2"}
|
||||
t.Log(a)
|
||||
clear(a)
|
||||
t.Log(a)
|
||||
}
|
Loading…
Reference in new issue