@ -2,11 +2,14 @@ package tools
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
kdisc "github.com/openimsdk/open-im-server/v3/pkg/common/discovery"
disetcd "github.com/openimsdk/open-im-server/v3/pkg/common/discovery/etcd"
pbconversation "github.com/openimsdk/protocol/conversation"
"github.com/openimsdk/protocol/msg"
"github.com/openimsdk/protocol/third"
"github.com/openimsdk/tools/discovery/etcd"
"github.com/openimsdk/tools/mcontext"
"github.com/openimsdk/tools/mw"
@ -27,38 +30,47 @@ type CronTaskConfig struct {
runTimeEnv string
}
func Start ( ctx context . Context , conf ig * CronTaskConfig ) error {
conf ig . runTimeEnv = runtimeenv . PrintRuntimeEnvironment ( )
func Start ( ctx context . Context , conf * CronTaskConfig ) error {
conf . runTimeEnv = runtimeenv . PrintRuntimeEnvironment ( )
log . CInfo ( ctx , "CRON-TASK server is initializing" , "runTimeEnv" , conf ig . runTimeEnv , "chatRecordsClearTime" , conf ig . CronTask . CronExecuteTime , "msgDestructTime" , conf ig . CronTask . RetainChatRecords )
if conf ig . CronTask . RetainChatRecords < 1 {
log . CInfo ( ctx , "CRON-TASK server is initializing" , "runTimeEnv" , conf . runTimeEnv , "chatRecordsClearTime" , conf . CronTask . CronExecuteTime , "msgDestructTime" , conf . CronTask . RetainChatRecords )
if conf . CronTask . RetainChatRecords < 1 {
return errs . New ( "msg destruct time must be greater than 1" ) . Wrap ( )
}
client , err := kdisc . NewDiscoveryRegister ( & conf ig . Discovery , conf ig . runTimeEnv )
client , err := kdisc . NewDiscoveryRegister ( & conf . Discovery , conf . runTimeEnv , nil )
if err != nil {
return errs . WrapMsg ( err , "failed to register discovery service" )
}
client . AddOption ( mw . GrpcClient ( ) , grpc . WithTransportCredentials ( insecure . NewCredentials ( ) ) )
ctx = mcontext . SetOpUserID ( ctx , conf ig . Share . IMAdminUserID [ 0 ] )
ctx = mcontext . SetOpUserID ( ctx , conf . Share . IMAdminUserID [ 0 ] )
msgConn , err := client . GetConn ( ctx , conf ig . Discovery . RpcService . Msg )
msgConn , err := client . GetConn ( ctx , conf . Discovery . RpcService . Msg )
if err != nil {
return err
}
thirdConn , err := client . GetConn ( ctx , conf ig . Discovery . RpcService . Third )
thirdConn , err := client . GetConn ( ctx , conf . Discovery . RpcService . Third )
if err != nil {
return err
}
conversationConn , err := client . GetConn ( ctx , conf ig . Discovery . RpcService . Conversation )
conversationConn , err := client . GetConn ( ctx , conf . Discovery . RpcService . Conversation )
if err != nil {
return err
}
if conf . Discovery . Enable == config . ETCD {
cm := disetcd . NewConfigManager ( client . ( * etcd . SvcDiscoveryRegistryImpl ) . GetClient ( ) , [ ] string {
conf . CronTask . GetConfigFileName ( ) ,
conf . Share . GetConfigFileName ( ) ,
conf . Discovery . GetConfigFileName ( ) ,
} )
cm . Watch ( ctx )
}
srv := & cronServer {
ctx : ctx ,
config : config ,
config : conf ,
cron : cron . New ( ) ,
msgClient : msg . NewMsgClient ( msgConn ) ,
conversationClient : pbconversation . NewConversationClient ( conversationConn ) ,
@ -74,7 +86,7 @@ func Start(ctx context.Context, config *CronTaskConfig) error {
if err := srv . registerClearUserMsg ( ) ; err != nil {
return err
}
log . ZDebug ( ctx , "start cron task" , "CronExecuteTime" , conf ig . CronTask . CronExecuteTime )
log . ZDebug ( ctx , "start cron task" , "CronExecuteTime" , conf . CronTask . CronExecuteTime )
srv . cron . Start ( )
<- ctx . Done ( )
return nil