optimization: change the configuration file from being read globally … (#1935)

* optimization: change the configuration file from being read globally to being read independently.

* optimization: change the configuration file from being read globally to being read independently.

* optimization: change the configuration file from being read globally to being read independently.

* optimization: config file changed to dependency injection.

* fix: replace global config with dependency injection

* fix: replace global config with dependency injection

* fix: import the enough param

* fix: import the enough param

* fix: import the enough param

* fix: fix the component check of path

* fix: fix the kafka of tls is nil problem

* fix: fix the TLS.CACrt is nil error

* fix: fix the valiable shadows problem

* fix: fix the comflect

* optimization: message remove options.

* fix: fix the param pass error

* fix: find error

* fix: find error

* fix: find eror

* fix: find error

* fix: find error

* fix: del the undifined func

* fix: find error

* fix: fix the error

* fix: pass config

* fix: find error

* fix: find error

* fix: find error

* fix: find error

* fix: find error

* fix: fix the config

* fix: fix the error

* fix: fix the config pass error

* fix: fix the eror

* fix: fix the error

* fix: fix the error

* fix: fix the error

* fix: find error

* fix: fix the error

* fix: fix the config

* fix: add return err

* fix: fix the err2

* fix: err

* fix: fix the func

* fix: del the chinese comment

* fix: fix the func

* fix: fix the gateway_test logic

* fix: s3

* test

* test

* fix: not found

---------

Co-authored-by: luhaoling <2198702716@qq.com>
Co-authored-by: withchao <993506633@qq.com>
This commit is contained in:
OpenIM-Gordon
2024-03-05 17:53:22 +08:00
committed by GitHub
parent efb8310531
commit 383758782e
114 changed files with 1743 additions and 1802 deletions
+13 -12
View File
@@ -23,37 +23,38 @@ import (
"time"
"github.com/OpenIMSDK/tools/errs"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
"github.com/redis/go-redis/v9"
"github.com/robfig/cron/v3"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
)
func StartTask() error {
fmt.Println("Cron task start, config:", config.Config.ChatRecordsClearTime)
func StartTask(config *config.GlobalConfig) error {
fmt.Println("cron task start, config", config.ChatRecordsClearTime)
msgTool, err := InitMsgTool()
msgTool, err := InitMsgTool(config)
if err != nil {
return err
}
msgTool.convertTools()
rdb, err := cache.NewRedis()
rdb, err := cache.NewRedis(config)
if err != nil {
return err
}
// register cron tasks
var crontab = cron.New()
fmt.Printf("Start chatRecordsClearTime cron task, cron config: %s\n", config.Config.ChatRecordsClearTime)
_, err = crontab.AddFunc(config.Config.ChatRecordsClearTime, cronWrapFunc(rdb, "cron_clear_msg_and_fix_seq", msgTool.AllConversationClearMsgAndFixSeq))
fmt.Printf("Start chatRecordsClearTime cron task, cron config: %s\n", config.ChatRecordsClearTime)
_, err = crontab.AddFunc(config.ChatRecordsClearTime, cronWrapFunc(config,rdb, "cron_clear_msg_and_fix_seq", msgTool.AllConversationClearMsgAndFixSeq))
if err != nil {
return errs.Wrap(err)
}
fmt.Printf("Start msgDestruct cron task, cron config: %s\n", config.Config.MsgDestructTime)
_, err = crontab.AddFunc(config.Config.MsgDestructTime, cronWrapFunc(rdb, "cron_conversations_destruct_msgs", msgTool.ConversationsDestructMsgs))
fmt.Printf("Start msgDestruct cron task, cron config: %s\n", config.MsgDestructTime)
_, err = crontab.AddFunc(config.MsgDestructTime, cronWrapFunc(config,rdb, "cron_conversations_destruct_msgs", msgTool.ConversationsDestructMsgs))
if err != nil {
return errs.Wrap(err, "cron_conversations_destruct_msgs")
}
@@ -91,8 +92,8 @@ func netlock(rdb redis.UniversalClient, key string, ttl time.Duration) bool {
return ok
}
func cronWrapFunc(rdb redis.UniversalClient, key string, fn func()) func() {
enableCronLocker := config.Config.EnableCronLocker
func cronWrapFunc(config *config.GlobalConfig, rdb redis.UniversalClient, key string, fn func()) func() {
enableCronLocker := config.EnableCronLocker
return func() {
// if don't enable cron-locker, call fn directly.
if !enableCronLocker {
+31 -4
View File
@@ -15,8 +15,12 @@
package tools
import (
"flag"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"gopkg.in/yaml.v3"
"math/rand"
"os"
"sync"
"testing"
"time"
@@ -61,7 +65,7 @@ func TestCronWrapFunc(t *testing.T) {
start := time.Now()
key := fmt.Sprintf("cron-%v", rand.Int31())
crontab := cron.New(cron.WithSeconds())
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(rdb, key, cb))
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(config.NewGlobalConfig(), rdb, key, cb))
crontab.Start()
<-done
@@ -71,7 +75,11 @@ func TestCronWrapFunc(t *testing.T) {
}
func TestCronWrapFuncWithNetlock(t *testing.T) {
config.Config.EnableCronLocker = true
conf, err := initCfg()
if err != nil {
panic(err)
}
conf.EnableCronLocker = true
rdb := redis.NewClient(&redis.Options{})
defer rdb.Close()
@@ -80,10 +88,10 @@ func TestCronWrapFuncWithNetlock(t *testing.T) {
crontab := cron.New(cron.WithSeconds())
key := fmt.Sprintf("cron-%v", rand.Int31())
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(rdb, key, func() {
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(conf, rdb, key, func() {
done <- "host1"
}))
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(rdb, key, func() {
crontab.AddFunc("*/1 * * * * *", cronWrapFunc(conf, rdb, key, func() {
done <- "host2"
}))
crontab.Start()
@@ -94,3 +102,22 @@ func TestCronWrapFuncWithNetlock(t *testing.T) {
crontab.Stop()
}
func initCfg() (*config.GlobalConfig, error) {
const (
defaultCfgPath = "../../../../../config/config.yaml"
)
cfgPath := flag.String("c", defaultCfgPath, "Path to the configuration file")
data, err := os.ReadFile(*cfgPath)
if err != nil {
return nil, errs.Wrap(err, "ReadFile unmarshal failed")
}
conf := config.NewGlobalConfig()
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, errs.Wrap(err, "InitConfig unmarshal failed")
}
return conf, nil
}
+20 -17
View File
@@ -46,10 +46,12 @@ type MsgTool struct {
userDatabase controller.UserDatabase
groupDatabase controller.GroupDatabase
msgNotificationSender *notification.MsgNotificationSender
Config *config.GlobalConfig
}
func NewMsgTool(msgDatabase controller.CommonMsgDatabase, userDatabase controller.UserDatabase,
groupDatabase controller.GroupDatabase, conversationDatabase controller.ConversationDatabase, msgNotificationSender *notification.MsgNotificationSender,
groupDatabase controller.GroupDatabase, conversationDatabase controller.ConversationDatabase,
msgNotificationSender *notification.MsgNotificationSender, config *config.GlobalConfig,
) *MsgTool {
return &MsgTool{
msgDatabase: msgDatabase,
@@ -57,32 +59,33 @@ func NewMsgTool(msgDatabase controller.CommonMsgDatabase, userDatabase controlle
groupDatabase: groupDatabase,
conversationDatabase: conversationDatabase,
msgNotificationSender: msgNotificationSender,
Config: config,
}
}
func InitMsgTool() (*MsgTool, error) {
rdb, err := cache.NewRedis()
func InitMsgTool(config *config.GlobalConfig) (*MsgTool, error) {
rdb, err := cache.NewRedis(config)
if err != nil {
return nil, err
}
mongo, err := unrelation.NewMongo()
mongo, err := unrelation.NewMongo(config)
if err != nil {
return nil, err
}
discov, err := kdisc.NewDiscoveryRegister(config.Config.Envs.Discovery)
discov, err := kdisc.NewDiscoveryRegister(config)
if err != nil {
return nil, err
}
discov.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, "round_robin")))
userDB, err := mgo.NewUserMongo(mongo.GetDatabase())
userDB, err := mgo.NewUserMongo(mongo.GetDatabase(config.Mongo.Database))
if err != nil {
return nil, err
}
msgDatabase, err := controller.InitCommonMsgDatabase(rdb, mongo.GetDatabase())
msgDatabase, err := controller.InitCommonMsgDatabase(rdb, mongo.GetDatabase(config.Mongo.Database), config)
if err != nil {
return nil, err
}
userMongoDB := unrelation.NewUserMongoDriver(mongo.GetDatabase())
userMongoDB := unrelation.NewUserMongoDriver(mongo.GetDatabase(config.Mongo.Database))
ctxTx := tx.NewMongo(mongo.GetClient())
userDatabase := controller.NewUserDatabase(
userDB,
@@ -90,19 +93,19 @@ func InitMsgTool() (*MsgTool, error) {
ctxTx,
userMongoDB,
)
groupDB, err := mgo.NewGroupMongo(mongo.GetDatabase())
groupDB, err := mgo.NewGroupMongo(mongo.GetDatabase(config.Mongo.Database))
if err != nil {
return nil, err
}
groupMemberDB, err := mgo.NewGroupMember(mongo.GetDatabase())
groupMemberDB, err := mgo.NewGroupMember(mongo.GetDatabase(config.Mongo.Database))
if err != nil {
return nil, err
}
groupRequestDB, err := mgo.NewGroupRequestMgo(mongo.GetDatabase())
groupRequestDB, err := mgo.NewGroupRequestMgo(mongo.GetDatabase(config.Mongo.Database))
if err != nil {
return nil, err
}
conversationDB, err := mgo.NewConversationMongo(mongo.GetDatabase())
conversationDB, err := mgo.NewConversationMongo(mongo.GetDatabase(config.Mongo.Database))
if err != nil {
return nil, err
}
@@ -112,9 +115,9 @@ func InitMsgTool() (*MsgTool, error) {
cache.NewConversationRedis(rdb, cache.GetDefaultOpt(), conversationDB),
ctxTx,
)
msgRpcClient := rpcclient.NewMessageRpcClient(discov)
msgNotificationSender := notification.NewMsgNotificationSender(rpcclient.WithRpcClient(&msgRpcClient))
msgTool := NewMsgTool(msgDatabase, userDatabase, groupDatabase, conversationDatabase, msgNotificationSender)
msgRpcClient := rpcclient.NewMessageRpcClient(discov, config)
msgNotificationSender := notification.NewMsgNotificationSender(config, rpcclient.WithRpcClient(&msgRpcClient))
msgTool := NewMsgTool(msgDatabase, userDatabase, groupDatabase, conversationDatabase, msgNotificationSender, config)
return msgTool, nil
}
@@ -176,8 +179,8 @@ func (c *MsgTool) AllConversationClearMsgAndFixSeq() {
func (c *MsgTool) ClearConversationsMsg(ctx context.Context, conversationIDs []string) {
for _, conversationID := range conversationIDs {
if err := c.msgDatabase.DeleteConversationMsgsAndSetMinSeq(ctx, conversationID, int64(config.Config.RetainChatRecords*24*60*60)); err != nil {
log.ZError(ctx, "DeleteUserSuperGroupMsgsAndSetMinSeq failed", err, "conversationID", conversationID, "DBRetainChatRecords", config.Config.RetainChatRecords)
if err := c.msgDatabase.DeleteConversationMsgsAndSetMinSeq(ctx, conversationID, int64(c.Config.RetainChatRecords*24*60*60)); err != nil {
log.ZError(ctx, "DeleteUserSuperGroupMsgsAndSetMinSeq failed", err, "conversationID", conversationID, "DBRetainChatRecords", c.Config.RetainChatRecords)
}
if err := c.checkMaxSeq(ctx, conversationID); err != nil {
log.ZError(ctx, "fixSeq failed", err, "conversationID", conversationID)