From 36b970844e189303df67978764f2f28b69804d4f Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Tue, 30 Nov 2021 15:15:57 +0800 Subject: [PATCH] MustLoadConfig refactored --- .gitignore | 2 +- ...{launch.sample.json => launch.json.sample} | 0 ...tings.sample.json => settings.json.sample} | 0 app/main.go | 2 ++ common/types.go | 31 +++++++++---------- common/types_test.go | 1 + dtmsvr/utils.go | 2 +- dtmsvr/utils_test.go | 2 ++ examples/data.go | 2 +- test/main_test.go | 1 + test/types.go | 2 +- 11 files changed, 24 insertions(+), 21 deletions(-) rename .vscode/{launch.sample.json => launch.json.sample} (100%) rename .vscode/{settings.sample.json => settings.json.sample} (100%) diff --git a/.gitignore b/.gitignore index 7bec55c..b8d2ee4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ conf.yml main dist .idea/** -.vscode/** +.vscode/*.json diff --git a/.vscode/launch.sample.json b/.vscode/launch.json.sample similarity index 100% rename from .vscode/launch.sample.json rename to .vscode/launch.json.sample diff --git a/.vscode/settings.sample.json b/.vscode/settings.json.sample similarity index 100% rename from .vscode/settings.sample.json rename to .vscode/settings.json.sample diff --git a/app/main.go b/app/main.go index 7eb0025..e0827ba 100644 --- a/app/main.go +++ b/app/main.go @@ -48,6 +48,8 @@ func main() { fmt.Printf("version: %s commit: %s built at: %s\n", Version, Commit, Date) return } + dtmimp.Logf("starting dtm....") + common.MustLoadConfig() dtmcli.SetCurrentDBType(common.DtmConfig.DB["driver"]) if os.Args[1] != "dtmsvr" { // 实际线上运行,只启动dtmsvr,不准备table相关的数据 common.WaitDBUp() diff --git a/common/types.go b/common/types.go index c39f5b1..790609b 100644 --- a/common/types.go +++ b/common/types.go @@ -8,6 +8,7 @@ package common import ( "database/sql" + "errors" "fmt" "io/ioutil" "os" @@ -145,10 +146,7 @@ func getIntEnv(key string, defaultV string) int64 { return int64(dtmimp.MustAtoi(dtmimp.OrString(os.Getenv(key), defaultV))) } -func init() { - if len(os.Args) == 1 || os.Args[1] == "version" { - return - } +func MustLoadConfig() { DtmConfig.TransCronInterval = getIntEnv("TRANS_CRON_INTERVAL", "3") DtmConfig.TimeoutToFail = getIntEnv("TIMEOUT_TO_FAIL", "35") DtmConfig.RetryInterval = getIntEnv("RETRY_INTERVAL", "10") @@ -177,26 +175,25 @@ func init() { err := yaml.Unmarshal(cont, &DtmConfig) dtmimp.FatalIfError(err) } - errStr := checkConfig() - dtmimp.LogIfFatalf(errStr != "", - `config error: '%s'. -check you env, and conf.yml/conf.sample.yml in current and parent path: %s. -please visit http://d.dtm.pub to see the config document. -loaded config is: -%v`, MustGetwd(), DtmConfig) + err := checkConfig() + dtmimp.LogIfFatalf(err != nil, `config error: '%v'. + check you env, and conf.yml/conf.sample.yml in current and parent path: %s. + please visit http://d.dtm.pub to see the config document. + loaded config is: + %v`, err, MustGetwd(), DtmConfig) } -func checkConfig() string { +func checkConfig() error { if DtmConfig.DB["driver"] == "" { - return "db driver empty" + return errors.New("db driver empty") } else if DtmConfig.DB["user"] == "" || DtmConfig.DB["host"] == "" { - return "db config not valid" + return errors.New("db config not valid") } else if DtmConfig.RetryInterval < 10 { - return "RetryInterval should not be less than 10" + return errors.New("RetryInterval should not be less than 10") } else if DtmConfig.TimeoutToFail < DtmConfig.RetryInterval { - return "TimeoutToFail should not be less than RetryInterval" + return errors.New("TimeoutToFail should not be less than RetryInterval") } - return "" + return nil } // WaitDBUp wait for db to go up diff --git a/common/types_test.go b/common/types_test.go index 5933dd1..353b4ed 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -14,6 +14,7 @@ import ( ) func TestDb(t *testing.T) { + MustLoadConfig() db := DbGet(DtmConfig.DB) err := func() (rerr error) { defer dtmimp.P2E(&rerr) diff --git a/dtmsvr/utils.go b/dtmsvr/utils.go index c41f889..79f8263 100644 --- a/dtmsvr/utils.go +++ b/dtmsvr/utils.go @@ -30,7 +30,7 @@ type branchStatus struct { var p2e = dtmimp.P2E var e2p = dtmimp.E2P -var config = common.DtmConfig +var config = &common.DtmConfig func dbGet() *common.DB { return common.DbGet(config.DB) diff --git a/dtmsvr/utils_test.go b/dtmsvr/utils_test.go index 968d2be..08b1acd 100644 --- a/dtmsvr/utils_test.go +++ b/dtmsvr/utils_test.go @@ -10,10 +10,12 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli/dtmimp" ) func TestUtils(t *testing.T) { + common.MustLoadConfig() db := dbGet() db.NoMust() err := dtmimp.CatchP(func() { diff --git a/examples/data.go b/examples/data.go index 390a8ca..7d22d3d 100644 --- a/examples/data.go +++ b/examples/data.go @@ -15,7 +15,7 @@ import ( "github.com/yedf/dtm/dtmcli/dtmimp" ) -var config = common.DtmConfig +var config = &common.DtmConfig // RunSQLScript 1 func RunSQLScript(conf map[string]string, script string, skipDrop bool) { diff --git a/test/main_test.go b/test/main_test.go index 1ec8919..251d4e5 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -17,6 +17,7 @@ import ( ) func TestMain(m *testing.M) { + common.MustLoadConfig() dtmcli.SetCurrentDBType(common.DtmConfig.DB["driver"]) dtmsvr.TransProcessedTestChan = make(chan string, 1) dtmsvr.NowForwardDuration = 0 * time.Second diff --git a/test/types.go b/test/types.go index 7de05b0..399cb6b 100644 --- a/test/types.go +++ b/test/types.go @@ -15,7 +15,7 @@ import ( "github.com/yedf/dtm/dtmsvr" ) -var config = common.DtmConfig +var config = &common.DtmConfig func dbGet() *common.DB { return common.DbGet(config.DB)