Browse Source

Merge pull request #66 from yedf/alpha

MustLoadConfig refactored
pull/67/head
yedf2 4 years ago
committed by GitHub
parent
commit
276a0e6577
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .gitignore
  2. 0
      .vscode/launch.json.sample
  3. 0
      .vscode/settings.json.sample
  4. 2
      app/main.go
  5. 31
      common/types.go
  6. 1
      common/types_test.go
  7. 2
      dtmsvr/utils.go
  8. 2
      dtmsvr/utils_test.go
  9. 2
      examples/data.go
  10. 1
      test/main_test.go
  11. 2
      test/types.go

2
.gitignore

@ -5,4 +5,4 @@ conf.yml
main
dist
.idea/**
.vscode/**
.vscode/*.json

0
.vscode/launch.sample.json → .vscode/launch.json.sample

0
.vscode/settings.sample.json → .vscode/settings.json.sample

2
app/main.go

@ -50,6 +50,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()

31
common/types.go

@ -8,6 +8,7 @@ package common
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
@ -164,10 +165,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")
@ -199,26 +197,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

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

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

2
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() {

2
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) {

1
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

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

Loading…
Cancel
Save