From 81e4edf11fd484e3241b1878beb85d10de1eda38 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 8 Nov 2021 14:05:35 +0800 Subject: [PATCH] fix wait db up --- app/main.go | 1 + common/types.go | 10 ++++++++++ examples/data.go | 7 ------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/main.go b/app/main.go index d628c97..1fa99bd 100644 --- a/app/main.go +++ b/app/main.go @@ -37,6 +37,7 @@ func main() { } dtmcli.SetCurrentDBType(common.DtmConfig.DB["driver"]) if os.Args[1] != "dtmsvr" { // 实际线上运行,只启动dtmsvr,不准备table相关的数据 + common.WaitDBUp() dtmsvr.PopulateDB(true) examples.PopulateDB(true) } diff --git a/common/types.go b/common/types.go index b9fa33e..4f7f74a 100644 --- a/common/types.go +++ b/common/types.go @@ -192,3 +192,13 @@ func checkConfig() string { } return "" } + +// WaitDBUp wait for db to go up +func WaitDBUp() { + sdb, err := dtmimp.StandaloneDB(DtmConfig.DB) + dtmimp.FatalIfError(err) + for _, err := dtmimp.DBExec(sdb, "select 1"); err != nil; { // wait for mysql to start + time.Sleep(3 * time.Second) + _, err = dtmimp.DBExec(sdb, "select 1") + } +} diff --git a/examples/data.go b/examples/data.go index c32362e..60a03f2 100644 --- a/examples/data.go +++ b/examples/data.go @@ -4,7 +4,6 @@ import ( "fmt" "io/ioutil" "strings" - "time" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli/dtmimp" @@ -48,12 +47,6 @@ func resetXaData() { // PopulateDB populate example mysql data func PopulateDB(skipDrop bool) { - sdb := sdbGet() - for _, err := dtmimp.DBExec(sdb, "select 1"); err != nil; { // wait for mysql to start - time.Sleep(3 * time.Second) - _, err = dtmimp.DBExec(sdb, "select 1") - } - resetXaData() file := fmt.Sprintf("%s/examples.%s.sql", common.GetCallerCodeDir(), config.DB["driver"]) RunSQLScript(config.DB, file, skipDrop)