mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-08 19:16:35 +08:00
refactoring scheduled tasks
This commit is contained in:
@@ -74,6 +74,8 @@ type ConversationDatabase interface {
|
||||
GetNotNotifyConversationIDs(ctx context.Context, userID string) ([]string, error)
|
||||
// GetPinnedConversationIDs gets pinned conversationIDs by userID
|
||||
GetPinnedConversationIDs(ctx context.Context, userID string) ([]string, error)
|
||||
// FindRandConversation finds random conversations based on the specified timestamp and limit.
|
||||
FindRandConversation(ctx context.Context, ts int64, limit int) ([]*relationtb.Conversation, error)
|
||||
}
|
||||
|
||||
func NewConversationDatabase(conversation database.Conversation, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
|
||||
@@ -401,3 +403,7 @@ func (c *conversationDatabase) GetPinnedConversationIDs(ctx context.Context, use
|
||||
}
|
||||
return conversationIDs, nil
|
||||
}
|
||||
|
||||
func (c *conversationDatabase) FindRandConversation(ctx context.Context, ts int64, limit int) ([]*relationtb.Conversation, error) {
|
||||
return c.conversationDB.FindRandConversation(ctx, ts, limit)
|
||||
}
|
||||
|
||||
@@ -103,6 +103,8 @@ type CommonMsgDatabase interface {
|
||||
SetUserConversationsMinSeq(ctx context.Context, conversationID string, userID string, seq int64) error
|
||||
|
||||
DeleteDoc(ctx context.Context, docID string) error
|
||||
|
||||
GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error)
|
||||
}
|
||||
|
||||
func NewCommonMsgDatabase(msgDocModel database.Msg, msg cache.MsgCache, seqUser cache.SeqUser, seqConversation cache.SeqConversationCache, kafkaConf *config.Kafka) (CommonMsgDatabase, error) {
|
||||
@@ -1016,3 +1018,7 @@ func (db *commonMsgDatabase) GetMaxSeqsWithTime(ctx context.Context, conversatio
|
||||
func (db *commonMsgDatabase) DeleteDoc(ctx context.Context, docID string) error {
|
||||
return db.msgDocDatabase.DeleteDoc(ctx, docID)
|
||||
}
|
||||
|
||||
func (db *commonMsgDatabase) GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error) {
|
||||
return db.msgDocDatabase.GetLastMessageSeqByTime(ctx, conversationID, time)
|
||||
}
|
||||
|
||||
@@ -42,4 +42,5 @@ type Conversation interface {
|
||||
GetConversationIDsNeedDestruct(ctx context.Context) ([]*model.Conversation, error)
|
||||
GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error)
|
||||
FindConversationUserVersion(ctx context.Context, userID string, version uint, limit int) (*model.VersionLog, error)
|
||||
FindRandConversation(ctx context.Context, ts int64, limit int) ([]*model.Conversation, error)
|
||||
}
|
||||
|
||||
@@ -228,3 +228,35 @@ func (c *ConversationMgo) GetConversationNotReceiveMessageUserIDs(ctx context.Co
|
||||
func (c *ConversationMgo) FindConversationUserVersion(ctx context.Context, userID string, version uint, limit int) (*model.VersionLog, error) {
|
||||
return c.version.FindChangeLog(ctx, userID, version, limit)
|
||||
}
|
||||
|
||||
func (c *ConversationMgo) FindRandConversation(ctx context.Context, ts int64, limit int) ([]*model.Conversation, error) {
|
||||
pipeline := []bson.M{
|
||||
{
|
||||
"$match": bson.M{
|
||||
"is_msg_destruct": true,
|
||||
"msg_destruct_time": bson.M{"$ne": 0},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$addFields": bson.M{
|
||||
"next_msg_destruct_timestamp": bson.M{
|
||||
"$add": []any{
|
||||
bson.M{
|
||||
"$toLong": "$latest_msg_destruct_time",
|
||||
}, "$msg_destruct_time"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$match": bson.M{
|
||||
"next_msg_destruct_timestamp": bson.M{"$lt": ts},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sample": bson.M{
|
||||
"size": limit,
|
||||
},
|
||||
},
|
||||
}
|
||||
return mongoutil.Aggregate[*model.Conversation](ctx, c.coll, pipeline)
|
||||
}
|
||||
|
||||
@@ -1290,7 +1290,9 @@ func (m *MsgMgo) GetRandBeforeMsg(ctx context.Context, ts int64, limit int) ([]*
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sample": limit,
|
||||
"$sample": bson.M{
|
||||
"size": limit,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1307,53 +1309,56 @@ func (m *MsgMgo) DeleteMsgByIndex(ctx context.Context, docID string, index []int
|
||||
return mongoutil.UpdateOne(ctx, m.coll, bson.M{"doc_id": docID}, bson.M{"$set": set}, true)
|
||||
}
|
||||
|
||||
//func (m *MsgMgo) ClearMsg(ctx context.Context, t time.Time) (int64, error) {
|
||||
// ts := t.UnixMilli()
|
||||
// var count int64
|
||||
// for {
|
||||
// msgs, err := m.GetBeforeMsg(ctx, ts, 100)
|
||||
// if err != nil {
|
||||
// return count, err
|
||||
// }
|
||||
// if len(msgs) == 0 {
|
||||
// return count, nil
|
||||
// }
|
||||
// for _, msg := range msgs {
|
||||
// num, err := m.deleteOneMsg(ctx, ts, msg)
|
||||
// count += num
|
||||
// if err != nil {
|
||||
// return count, err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
func (m *MsgMgo) DeleteDoc(ctx context.Context, docID string) error {
|
||||
return mongoutil.DeleteOne(ctx, m.coll, bson.M{"doc_id": docID})
|
||||
}
|
||||
|
||||
//func (m *MsgMgo) DeleteDocMsg(ctx context.Context, ts int64, doc *relation.MsgDocModel) (int64, error) {
|
||||
// var notNull int
|
||||
// index := make([]int, 0, len(doc.Msg))
|
||||
// for i, message := range doc.Msg {
|
||||
// if message.Msg != nil {
|
||||
// notNull++
|
||||
// if message.Msg.SendTime < ts {
|
||||
// index = append(index, i)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if len(index) == 0 {
|
||||
// return 0, errs.New("no msg to delete").WrapMsg("deleteOneMsg", "docID", doc.DocID)
|
||||
// }
|
||||
// if len(index) == notNull {
|
||||
// if err := m.DeleteDoc(ctx, doc.DocID); err != nil {
|
||||
// return 0, err
|
||||
// }
|
||||
// } else {
|
||||
// if err := m.setNullMsg(ctx, doc.DocID, index); err != nil {
|
||||
// return 0, err
|
||||
// }
|
||||
// }
|
||||
// return int64(len(index)), nil
|
||||
//}
|
||||
func (m *MsgMgo) GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error) {
|
||||
pipeline := []bson.M{
|
||||
{
|
||||
"$match": bson.M{
|
||||
"doc_id": bson.M{
|
||||
"$regex": fmt.Sprintf("^%s:", conversationID),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$match": bson.M{
|
||||
"msgs.msg.send_time": bson.M{
|
||||
"msgs.msg.send_time": bson.M{
|
||||
"$lte": time,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sort": bson.M{
|
||||
"_id": -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$limit": 1,
|
||||
},
|
||||
{
|
||||
"$project": bson.M{
|
||||
"_id": 0,
|
||||
"msgs.msg.send_time": 1,
|
||||
"msgs.msg.seq": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
res, err := mongoutil.Aggregate[*model.MsgDocModel](ctx, m.coll, pipeline)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(res) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
var seq int64
|
||||
for _, v := range res[0].Msg {
|
||||
if v.Msg.SendTime <= time {
|
||||
seq = v.Msg.Seq
|
||||
}
|
||||
}
|
||||
return seq, nil
|
||||
}
|
||||
|
||||
@@ -15,10 +15,10 @@ import (
|
||||
)
|
||||
|
||||
func TestName1(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*300)
|
||||
defer cancel()
|
||||
cli := Result(mongo.Connect(ctx, options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.66:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second)))
|
||||
|
||||
//ctx, cancel := context.WithTimeout(context.Background(), time.Second*300)
|
||||
//defer cancel()
|
||||
//cli := Result(mongo.Connect(ctx, options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.66:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second)))
|
||||
//
|
||||
//v := &MsgMgo{
|
||||
// coll: cli.Database("openim_v3").Collection("msg3"),
|
||||
//}
|
||||
@@ -44,16 +44,16 @@ func TestName1(t *testing.T) {
|
||||
//}
|
||||
//
|
||||
//t.Log(total)
|
||||
|
||||
msg, err := NewMsgMongo(cli.Database("openim_v3"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
res, err := msg.GetBeforeMsg(ctx, time.Now().UnixMilli(), []string{"1:0"}, 1000)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Log(len(res))
|
||||
//
|
||||
//msg, err := NewMsgMongo(cli.Database("openim_v3"))
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//res, err := msg.GetBeforeMsg(ctx, time.Now().UnixMilli(), []string{"1:0"}, 1000)
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//t.Log(len(res))
|
||||
}
|
||||
|
||||
func TestName10(t *testing.T) {
|
||||
@@ -95,13 +95,20 @@ func TestName4(t *testing.T) {
|
||||
defer cancel()
|
||||
cli := Result(mongo.Connect(ctx, options.Client().ApplyURI("mongodb://openIM:openIM123@172.16.8.66:37017/openim_v3?maxPoolSize=100").SetConnectTimeout(5*time.Second)))
|
||||
|
||||
msg, err := NewMsgMongo(cli.Database("openim_v3"))
|
||||
msg, err := NewConversationMongo(cli.Database("openim_v3"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
res, err := msg.GetBeforeMsg(ctx, time.Now().UnixMilli(), []string{"1:0"}, 1000)
|
||||
ts := time.Now().UnixMilli()
|
||||
t.Log(ts)
|
||||
res, err := msg.FindRandConversation(ctx, ts, 10)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Log(len(res))
|
||||
t.Log(res)
|
||||
}
|
||||
|
||||
func TestName5(t *testing.T) {
|
||||
var v time.Time
|
||||
t.Log(v.UnixMilli())
|
||||
}
|
||||
|
||||
@@ -48,4 +48,6 @@ type Msg interface {
|
||||
GetRandBeforeMsg(ctx context.Context, ts int64, limit int) ([]*model.MsgDocModel, error)
|
||||
|
||||
GetRandDocIDs(ctx context.Context, limit int) ([]string, error)
|
||||
|
||||
GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user