2023-07-04 11:15:20 +08:00
// 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.
2023-06-30 11:24:37 +08:00
package tools
import (
"context"
2024-04-28 11:47:05 +08:00
"fmt"
2024-08-01 16:12:41 +08:00
"os"
"time"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2024-04-28 11:47:05 +08:00
kdisc "github.com/openimsdk/open-im-server/v3/pkg/common/discoveryregister"
2024-08-01 16:12:41 +08:00
pbconversation "github.com/openimsdk/protocol/conversation"
2024-04-28 11:47:05 +08:00
"github.com/openimsdk/protocol/msg"
2024-08-01 16:12:41 +08:00
2024-07-16 10:46:21 +08:00
"github.com/openimsdk/protocol/third"
2024-04-28 11:47:05 +08:00
"github.com/openimsdk/tools/mcontext"
"github.com/openimsdk/tools/mw"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
2023-06-30 11:24:37 +08:00
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
2024-03-06 15:58:05 +08:00
"github.com/robfig/cron/v3"
2023-06-30 11:24:37 +08:00
)
2024-04-19 22:23:08 +08:00
type CronTaskConfig struct {
2024-05-14 18:21:36 +08:00
CronTask config . CronTask
Share config . Share
Discovery config . Discovery
2024-04-19 22:23:08 +08:00
}
func Start ( ctx context . Context , config * CronTaskConfig ) error {
2024-07-16 10:46:21 +08:00
log . CInfo ( ctx , "CRON-TASK server is initializing" , "chatRecordsClearTime" , config . CronTask . CronExecuteTime , "msgDestructTime" , config . CronTask . RetainChatRecords )
2024-04-28 11:47:05 +08:00
if config . CronTask . RetainChatRecords < 1 {
return errs . New ( "msg destruct time must be greater than 1" ) . Wrap ( )
2023-11-13 15:21:54 +08:00
}
2024-05-14 18:21:36 +08:00
client , err := kdisc . NewDiscoveryRegister ( & config . Discovery , & config . Share )
2023-06-30 11:24:37 +08:00
if err != nil {
2024-04-28 11:47:05 +08:00
return errs . WrapMsg ( err , "failed to register discovery service" )
2023-07-12 15:31:24 +08:00
}
2024-04-28 11:47:05 +08:00
client . AddOption ( mw . GrpcClient ( ) , grpc . WithTransportCredentials ( insecure . NewCredentials ( ) ) )
ctx = mcontext . SetOpUserID ( ctx , config . Share . IMAdminUserID [ 0 ] )
2024-08-01 16:12:41 +08:00
msgConn , err := client . GetConn ( ctx , config . Share . RpcRegisterName . Msg )
2023-07-19 10:47:28 +08:00
if err != nil {
2024-04-28 11:47:05 +08:00
return err
2023-11-13 15:21:54 +08:00
}
2024-08-01 16:12:41 +08:00
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 )
2024-04-28 11:47:05 +08:00
crontab := cron . New ( )
2024-08-01 16:12:41 +08:00
// scheduled hard delete outdated Msgs in specific time.
clearMsgFunc := func ( ) {
2024-04-28 11:47:05 +08:00
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 . ZInfo ( ctx , "clear chat records" , "deltime" , deltime , "timestamp" , deltime . UnixMilli ( ) )
2024-08-01 16:12:41 +08:00
if _ , err := msgClient . ClearMsg ( ctx , & msg . ClearMsgReq { Timestamp : deltime . UnixMilli ( ) } ) ; err != nil {
2024-04-28 11:47:05 +08:00
log . ZError ( ctx , "cron clear chat records failed" , err , "deltime" , deltime , "cont" , time . Since ( now ) )
return
2023-11-13 15:21:54 +08:00
}
2024-04-28 11:47:05 +08:00
log . ZInfo ( ctx , "cron clear chat records success" , "deltime" , deltime , "cont" , time . Since ( now ) )
}
2024-08-01 16:12:41 +08:00
if _ , err := crontab . AddFunc ( config . CronTask . CronExecuteTime , clearMsgFunc ) ; err != nil {
2024-04-28 11:47:05 +08:00
return errs . Wrap ( err )
2023-11-13 15:21:54 +08:00
}
2024-07-16 10:46:21 +08:00
2024-08-01 16:12:41 +08:00
// 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 . ZInfo ( 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 . ZInfo ( ctx , "msg destruct cron task completed" , "cont" , time . Since ( now ) )
}
if _ , err := crontab . AddFunc ( config . CronTask . CronExecuteTime , msgDestructFunc ) ; err != nil {
return errs . Wrap ( err )
2024-07-16 10:46:21 +08:00
}
2024-08-01 16:12:41 +08:00
// scheduled delete outdated file Objects and their datas in specific time.
deleteObjectFunc := func ( ) {
2024-07-16 10:46:21 +08:00
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 . ZInfo ( 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 . ZInfo ( ctx , "cron deleteoutDatedData success" , "deltime" , deleteTime , "cont" , time . Since ( now ) )
}
2024-08-01 16:12:41 +08:00
if _ , err := crontab . AddFunc ( config . CronTask . CronExecuteTime , deleteObjectFunc ) ; err != nil {
2024-07-16 10:46:21 +08:00
return errs . Wrap ( err )
}
2024-08-01 16:12:41 +08:00
2024-07-16 10:46:21 +08:00
log . ZInfo ( ctx , "start cron task" , "CronExecuteTime" , config . CronTask . CronExecuteTime )
2024-04-28 11:47:05 +08:00
crontab . Start ( )
<- ctx . Done ( )
2024-05-07 21:05:41 +08:00
return nil
2023-11-13 15:21:54 +08:00
}