Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the component check logic to make it better #417

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
91 changes: 45 additions & 46 deletions tools/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,54 @@ package component

import (
"fmt"
"github.com/OpenIMSDK/tools/errs"
"strings"
"time"

"github.com/OpenIMSDK/chat/pkg/common/config"
component "github.com/OpenIMSDK/tools/component"
)

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))
}
}
}

Expand All @@ -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
//}
Loading