mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-09 11:35:59 +08:00
feat: optimize corn tasks (#2237)
* fix: GroupApplicationAcceptedNotification * fix: GroupApplicationAcceptedNotification * fix: NotificationUserInfoUpdate * cicd: robot automated Change * fix: component * fix: getConversationInfo * feat: cron task * feat: cron task * feat: cron task * feat: cron task * feat: cron task --------- Co-authored-by: withchao <withchao@users.noreply.github.com>
This commit is contained in:
@@ -536,3 +536,46 @@ func (c *conversationServer) GetConversationNotReceiveMessageUserIDs(ctx context
|
||||
}
|
||||
return &pbconversation.GetConversationNotReceiveMessageUserIDsResp{UserIDs: userIDs}, nil
|
||||
}
|
||||
|
||||
func (c *conversationServer) UpdateConversation(ctx context.Context, req *pbconversation.UpdateConversationReq) (*pbconversation.UpdateConversationResp, error) {
|
||||
m := make(map[string]any)
|
||||
if req.RecvMsgOpt != nil {
|
||||
m["recv_msg_opt"] = req.RecvMsgOpt.Value
|
||||
}
|
||||
if req.AttachedInfo != nil {
|
||||
m["attached_info"] = req.AttachedInfo.Value
|
||||
}
|
||||
if req.Ex != nil {
|
||||
m["ex"] = req.Ex.Value
|
||||
}
|
||||
if req.IsPinned != nil {
|
||||
m["is_pinned"] = req.IsPinned.Value
|
||||
}
|
||||
if req.GroupAtType != nil {
|
||||
m["group_at_type"] = req.GroupAtType.Value
|
||||
}
|
||||
if req.MsgDestructTime != nil {
|
||||
m["msg_destruct_time"] = req.MsgDestructTime.Value
|
||||
}
|
||||
if req.IsMsgDestruct != nil {
|
||||
m["is_msg_destruct"] = req.IsMsgDestruct.Value
|
||||
}
|
||||
if req.BurnDuration != nil {
|
||||
m["burn_duration"] = req.BurnDuration.Value
|
||||
}
|
||||
if req.IsPrivateChat != nil {
|
||||
m["is_private_chat"] = req.IsPrivateChat.Value
|
||||
}
|
||||
if req.MinSeq != nil {
|
||||
m["min_seq"] = req.MinSeq.Value
|
||||
}
|
||||
if req.MaxSeq != nil {
|
||||
m["max_seq"] = req.MaxSeq.Value
|
||||
}
|
||||
if len(m) > 0 {
|
||||
if err := c.conversationDatabase.UpdateUsersConversationField(ctx, req.UserIDs, req.ConversationID, m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &pbconversation.UpdateConversationResp{}, nil
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user