diff --git a/go.mod b/go.mod index 960e1713..2a4a2a17 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( require ( github.com/OpenIMSDK/protocol v0.0.21 - github.com/OpenIMSDK/tools v0.0.31 + github.com/OpenIMSDK/tools v0.0.36 github.com/livekit/protocol v1.5.0 github.com/redis/go-redis/v9 v9.2.1 github.com/xuri/excelize/v2 v2.8.0 diff --git a/go.sum b/go.sum index fdbebb14..83a0efd4 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/IBM/sarama v1.42.1 h1:wugyWa15TDEHh2kvq2gAy1IHLjEjuYOYgXz/ruC/OSQ= github.com/IBM/sarama v1.42.1/go.mod h1:Xxho9HkHd4K/MDUo/T/sOqwtX/17D33++E9Wib6hUdQ= github.com/OpenIMSDK/protocol v0.0.21 h1:5H6H+hJ9d/VgRqttvxD/zfK9Asd+4M8Eknk5swSbUVY= github.com/OpenIMSDK/protocol v0.0.21/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= -github.com/OpenIMSDK/tools v0.0.31 h1:fSrhcPTvHEMTSyrJZDupe730mL4nuhvSOUP/BaZiHaY= -github.com/OpenIMSDK/tools v0.0.31/go.mod h1:wBfR5CYmEyvxl03QJbTkhz1CluK6J4/lX0lviu8JAjE= +github.com/OpenIMSDK/tools v0.0.36 h1:BT0q64l4f3QJDW16Rc0uJYt1gQFkiPoUQYQ33vo0EcE= +github.com/OpenIMSDK/tools v0.0.36/go.mod h1:wBfR5CYmEyvxl03QJbTkhz1CluK6J4/lX0lviu8JAjE= github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 h1:iC9YFYKDGEy3n/FtqJnOkZsene9olVspKmkX5A2YBEo= github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4/go.mod h1:sCavSAvdzOjul4cEqeVtvlSaSScfNsTQ+46HwlTL1hc= github.com/alibabacloud-go/darabonba-openapi v0.1.18/go.mod h1:PB4HffMhJVmAgNKNq3wYbTUlFvPgxJpTzd1F5pTuUsc= diff --git a/tools/component/component.go b/tools/component/component.go index 7ec9a971..19ad8dbf 100644 --- a/tools/component/component.go +++ b/tools/component/component.go @@ -16,6 +16,8 @@ package component import ( "fmt" + "github.com/OpenIMSDK/tools/errs" + "strings" "time" "github.com/OpenIMSDK/chat/pkg/common/config" @@ -23,39 +25,45 @@ import ( ) var ( - MaxConnectTimes = 200 + MaxConnectTimes = 100 ) type checkFunc struct { name string - function func() (string, error) + function func() error + flag bool } func ComponentCheck() error { - var err error - var strInfo string if config.Config.Envs.Discovery != "k8s" { checks := []checkFunc{ {name: "Zookeeper", function: checkZookeeper}, {name: "Redis", function: checkRedis}, - {name: "MySQL", function: checkMySQL}, + //{name: "Mongo", function: checkMongo, config: conf}, } - for i := 0; i < component.MaxRetry; i++ { + for i := 0; i < MaxConnectTimes; i++ { if i != 0 { time.Sleep(1 * time.Second) } fmt.Printf("Checking components Round %v...\n", i+1) + var err error allSuccess := true - for _, check := range checks { - strInfo, err = check.function() - if err != nil { - component.ErrorPrint(fmt.Sprintf("Starting %s failed, %v", check.name, err)) - allSuccess = false - break - } else { - component.SuccessPrint(fmt.Sprintf("%s connected successfully, %s", check.name, strInfo)) + for index, check := range checks { + if !check.flag { + err = check.function() + if err != nil { + allSuccess = false + component.ErrorPrint(fmt.Sprintf("Starting %s failed:%v.", check.name, errs.Unwrap(err).Error())) + if !strings.Contains(errs.Unwrap(err).Error(), "connection refused") && + !strings.Contains(errs.Unwrap(err).Error(), "timeout waiting") { + return err + } + } else { + checks[index].flag = true + component.SuccessPrint(fmt.Sprintf("%s connected successfully", check.name)) + } } } @@ -65,52 +73,43 @@ func ComponentCheck() error { } } } - return err + return errs.Wrap(fmt.Errorf("components started failed")) } // checkZookeeper checks the Zookeeper connection -func checkZookeeper() (string, error) { - // Prioritize environment variables - zk := &component.Zookeeper{ +func checkZookeeper() error { + zkStu := &component.Zookeeper{ Schema: config.Config.Zookeeper.Schema, ZkAddr: config.Config.Zookeeper.ZkAddr, Username: config.Config.Zookeeper.Username, Password: config.Config.Zookeeper.Password, } - - str, err := component.CheckZookeeper(zk) - if err != nil { - return "", err - } - return str, nil + err := component.CheckZookeeper(zkStu) + return err } // checkRedis checks the Redis connection -func checkRedis() (string, error) { - redis := &component.Redis{ +func checkRedis() error { + redisStu := &component.Redis{ Address: *config.Config.Redis.Address, Username: config.Config.Redis.Username, Password: config.Config.Redis.Password, } - - str, err := component.CheckRedis(redis) - if err != nil { - return "", err - } - return str, nil + err := component.CheckRedis(redisStu) + return err } -func checkMySQL() (string, error) { - - mysql := &component.MySQL{ - Address: *config.Config.Mysql.Address, - Username: *config.Config.Mysql.Username, - Password: *config.Config.Mysql.Password, - Database: *config.Config.Mysql.Database, - } - str, err := component.CheckMySQL(mysql) - if err != nil { - return "", err - } - return str, nil -} +// checkMongo checks the MongoDB connection without retries +//func checkMongo(config *config.GlobalConfig) error { +// mongoStu := &component.Mongo{ +// URL: config.Mongo.Uri, +// Address: config.Mongo.Address, +// Database: config.Mongo.Database, +// Username: config.Mongo.Username, +// Password: config.Mongo.Password, +// MaxPoolSize: config.Mongo.MaxPoolSize, +// } +// err := component.CheckMongo(mongoStu) +// +// return err +//}