mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-10-30 16:02:17 +08:00 
			
		
		
		
	* refactor: refactor workflows contents. * add tool workflows. * update field. * fix: remove chat error. * Fix err. * fix error. * remove cn comment. * update workflows files. * update infra config. * move workflows. * feat: update bot. * fix: solve uncorrect outdated msg get. * update get docIDs logic. * update * update skip logic. * fix * update. * fix: delay deleteObject func. * remove unused content. * update log type. * feat: implement request batch count limit. * update * update * feat: add rocksTimeout * feat: wrap logs * feat: add logs * feat: listen config * feat: enable listen TIME_WAIT port * feat: add logs * feat: cache batch * chore: enable fullUserCache * feat: push rpc num * feat: push err * feat: with operationID * feat: sleep * feat: change 1s * feat: change log * feat: implement Getbatch in rpcCache. * feat: print getOnline cost * feat: change log * feat: change kafka and push config * feat: del interface * feat: fix err * feat: change config * feat: go mod * feat: change config * feat: change config * feat: add sleep in push * feat: warn logs * feat: logs * feat: logs * feat: change port * feat: start config * feat: remove port reuse * feat: prometheus config * feat: prometheus config * feat: prometheus config * feat: add long time send msg to grafana * feat: init * feat: init * feat: implement offline push. * feat: batch get user online * feat: implement batch Push spilt * update go mod * Revert "feat: change port" This reverts commit 06d5e944 * feat: change port * feat: change config * feat: implement kafka producer and consumer. * update format, * add PushMQ log. * feat: get all online users and init push * feat: lock in online cache * feat: config * fix: init online status * fix: add logs * fix: userIDs * fix: add logs * feat: update Handler logic. * update MQ logic. * update * update * fix: method name * fix: update OfflinePushConsumerHandler. * fix: prommetrics * fix: add logs * fix: ctx * fix: log * fix: config * feat: change port * fix: atomic online cache status --------- Co-authored-by: Monet Lee <monet_lee@163.com>
		
			
				
	
	
		
			138 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // 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"
 | |
| 	"os"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/openimsdk/open-im-server/v3/pkg/common/config"
 | |
| 	kdisc "github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister"
 | |
| 	pbconversation "github.com/openimsdk/protocol/conversation"
 | |
| 	"github.com/openimsdk/protocol/msg"
 | |
| 
 | |
| 	"github.com/openimsdk/tools/mcontext"
 | |
| 	"github.com/openimsdk/tools/mw"
 | |
| 	"google.golang.org/grpc"
 | |
| 	"google.golang.org/grpc/credentials/insecure"
 | |
| 
 | |
| 	"github.com/openimsdk/tools/errs"
 | |
| 	"github.com/openimsdk/tools/log"
 | |
| 	"github.com/robfig/cron/v3"
 | |
| )
 | |
| 
 | |
| type CronTaskConfig struct {
 | |
| 	CronTask  config.CronTask
 | |
| 	Share     config.Share
 | |
| 	Discovery config.Discovery
 | |
| }
 | |
| 
 | |
| func Start(ctx context.Context, config *CronTaskConfig) error {
 | |
| 	log.CInfo(ctx, "CRON-TASK server is initializing", "chatRecordsClearTime", config.CronTask.CronExecuteTime, "msgDestructTime", config.CronTask.RetainChatRecords)
 | |
| 	if config.CronTask.RetainChatRecords < 1 {
 | |
| 		return errs.New("msg destruct time must be greater than 1").Wrap()
 | |
| 	}
 | |
| 	client, err := kdisc.NewDiscoveryRegister(&config.Discovery, &config.Share)
 | |
| 	if err != nil {
 | |
| 		return errs.WrapMsg(err, "failed to register discovery service")
 | |
| 	}
 | |
| 	client.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()))
 | |
| 	ctx = mcontext.SetOpUserID(ctx, config.Share.IMAdminUserID[0])
 | |
| 
 | |
| 	msgConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.Msg)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	// thirdConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.Third)
 | |
| 	// if err != nil {
 | |
| 	// 	return err
 | |
| 	// }
 | |
| 
 | |
| 	conversationConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.Conversation)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	msgClient := msg.NewMsgClient(msgConn)
 | |
| 	conversationClient := pbconversation.NewConversationClient(conversationConn)
 | |
| 	// thirdClient := third.NewThirdClient(thirdConn)
 | |
| 
 | |
| 	crontab := cron.New()
 | |
| 
 | |
| 	// scheduled hard delete outdated Msgs in specific time.
 | |
| 	clearMsgFunc := func() {
 | |
| 		now := time.Now()
 | |
| 		deltime := now.Add(-time.Hour * 24 * time.Duration(config.CronTask.RetainChatRecords))
 | |
| 		ctx := mcontext.SetOperationID(ctx, fmt.Sprintf("cron_%d_%d", os.Getpid(), deltime.UnixMilli()))
 | |
| 		log.ZDebug(ctx, "clear chat records", "deltime", deltime, "timestamp", deltime.UnixMilli())
 | |
| 
 | |
| 		if _, err := msgClient.ClearMsg(ctx, &msg.ClearMsgReq{Timestamp: deltime.UnixMilli()}); err != nil {
 | |
| 			log.ZError(ctx, "cron clear chat records failed", err, "deltime", deltime, "cont", time.Since(now))
 | |
| 			return
 | |
| 		}
 | |
| 		log.ZDebug(ctx, "cron clear chat records success", "deltime", deltime, "cont", time.Since(now))
 | |
| 	}
 | |
| 	if _, err := crontab.AddFunc(config.CronTask.CronExecuteTime, clearMsgFunc); err != nil {
 | |
| 		return errs.Wrap(err)
 | |
| 	}
 | |
| 
 | |
| 	// scheduled soft delete outdated Msgs in specific time when user set `is_msg_destruct` feature.
 | |
| 	msgDestructFunc := func() {
 | |
| 		now := time.Now()
 | |
| 		ctx := mcontext.SetOperationID(ctx, fmt.Sprintf("cron_%d_%d", os.Getpid(), now.UnixMilli()))
 | |
| 		log.ZDebug(ctx, "msg destruct cron start", "now", now)
 | |
| 
 | |
| 		conversations, err := conversationClient.GetConversationsNeedDestructMsgs(ctx, &pbconversation.GetConversationsNeedDestructMsgsReq{})
 | |
| 		if err != nil {
 | |
| 			log.ZError(ctx, "Get conversation need Destruct msgs failed.", err)
 | |
| 			return
 | |
| 		} else {
 | |
| 			_, err := msgClient.DestructMsgs(ctx, &msg.DestructMsgsReq{Conversations: conversations.Conversations})
 | |
| 			if err != nil {
 | |
| 				log.ZError(ctx, "Destruct Msgs failed.", err)
 | |
| 				return
 | |
| 			}
 | |
| 		}
 | |
| 		log.ZDebug(ctx, "msg destruct cron task completed", "cont", time.Since(now))
 | |
| 	}
 | |
| 	if _, err := crontab.AddFunc(config.CronTask.CronExecuteTime, msgDestructFunc); err != nil {
 | |
| 		return errs.Wrap(err)
 | |
| 	}
 | |
| 
 | |
| 	// // scheduled delete outdated file Objects and their datas in specific time.
 | |
| 	// deleteObjectFunc := func() {
 | |
| 	// 	now := time.Now()
 | |
| 	// 	deleteTime := now.Add(-time.Hour * 24 * time.Duration(config.CronTask.FileExpireTime))
 | |
| 	// 	ctx := mcontext.SetOperationID(ctx, fmt.Sprintf("cron_%d_%d", os.Getpid(), deleteTime.UnixMilli()))
 | |
| 	// 	log.ZDebug(ctx, "deleteoutDatedData ", "deletetime", deleteTime, "timestamp", deleteTime.UnixMilli())
 | |
| 	// 	if _, err := thirdClient.DeleteOutdatedData(ctx, &third.DeleteOutdatedDataReq{ExpireTime: deleteTime.UnixMilli()}); err != nil {
 | |
| 	// 		log.ZError(ctx, "cron deleteoutDatedData failed", err, "deleteTime", deleteTime, "cont", time.Since(now))
 | |
| 	// 		return
 | |
| 	// 	}
 | |
| 	// 	log.ZDebug(ctx, "cron deleteoutDatedData success", "deltime", deleteTime, "cont", time.Since(now))
 | |
| 	// }
 | |
| 	// if _, err := crontab.AddFunc(config.CronTask.CronExecuteTime, deleteObjectFunc); err != nil {
 | |
| 	// 	return errs.Wrap(err)
 | |
| 	// }
 | |
| 
 | |
| 	log.ZDebug(ctx, "start cron task", "CronExecuteTime", config.CronTask.CronExecuteTime)
 | |
| 	crontab.Start()
 | |
| 	<-ctx.Done()
 | |
| 	return nil
 | |
| }
 |