Skip to content

Commit

Permalink
Resolved #800 support test configuration
Browse files Browse the repository at this point in the history
Signed-off-by: kwanhur <[email protected]>
  • Loading branch information
kwanhur committed Nov 29, 2021
1 parent 285919b commit 8454396
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
28 changes: 24 additions & 4 deletions bfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
showVersion = flag.Bool("v", false, "to show version of bfe")
showVerbose = flag.Bool("V", false, "to show verbose information about bfe")
debugLog = flag.Bool("d", false, "to show debug log (otherwise >= info)")
testConf = flag.Bool("t", false, "test configuration and exit")
)

var version string
Expand Down Expand Up @@ -74,7 +75,12 @@ func main() {
logSwitch = "DEBUG"
bfe_debug.DebugIsOpen = true
} else {
logSwitch = "INFO"
// ignore under ERROR level
if *testConf {
logSwitch = "ERROR"
} else {
logSwitch = "INFO"
}
bfe_debug.DebugIsOpen = false
}

Expand All @@ -84,7 +90,7 @@ func main() {
log4go.SetLogFormat(log4go.FORMAT_DEFAULT_WITH_PID)
log4go.SetSrcLineForBinLog(false)

err = log.Init("bfe", logSwitch, *logPath, *stdOut, "midnight", 7)
err = log.Init("bfe", logSwitch, *logPath, *stdOut || *testConf, "midnight", 7)
if err != nil {
fmt.Printf("bfe: err in log.Init():%s\n", err.Error())
bfe_util.AbnormalExit()
Expand All @@ -97,10 +103,13 @@ func main() {
config, err = bfe_conf.BfeConfigLoad(confPath, *confRoot)
if err != nil {
log.Logger.Error("main(): in BfeConfigLoad():%s", err.Error())
if *testConf {
fmt.Printf("bfe: configuration file %s test failed\n", confPath)
}
bfe_util.AbnormalExit()
}

// maximum number of CPUs (GOMAXPROCS) defaults to runtime.CPUNUM
// maximum number of CPUs (GOMAXPROCS) defaults to runtime.CPUNUM
// if running on machine, or CPU quota if running on container
// (with the help of "go.uber.org/automaxprocs").
// here, we change maximum number of cpus if the MaxCpus is positive.
Expand All @@ -112,11 +121,22 @@ func main() {
bfe_debug.SetDebugFlag(config.Server)

// start and serve
if err = bfe_server.StartUp(config, version, *confRoot); err != nil {
if err = bfe_server.StartUp(config, version, *confRoot, *testConf); err != nil {
log.Logger.Error("main(): bfe_server.StartUp(): %s", err.Error())
}

// waiting for logger finish jobs
time.Sleep(1 * time.Second)
log.Logger.Close()

// output final configuration test result
if *testConf {
if err != nil {
fmt.Printf("bfe: configuration file %s test failed\n", confPath)
bfe_util.AbnormalExit()
} else {
fmt.Printf("bfe: configuration file %s test is successful\n", confPath)
bfe_util.NormalExit()
}
}
}
6 changes: 5 additions & 1 deletion bfe_server/bfe_server_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/bfenetworks/bfe/bfe_modules"
)

func StartUp(cfg bfe_conf.BfeConfig, version string, confRoot string) error {
func StartUp(cfg bfe_conf.BfeConfig, version string, confRoot string, dryRun bool) error {
var err error

// set all available modules
Expand Down Expand Up @@ -93,6 +93,10 @@ func StartUp(cfg bfe_conf.BfeConfig, version string, confRoot string) error {
}
log.Logger.Info("StartUp():bfeServer.InitPlugins() OK")

if dryRun {
return nil
}

// initialize listeners
if err = bfeServer.InitListeners(cfg); err != nil {
log.Logger.Error("StartUp(): InitListeners():%v", err)
Expand Down
14 changes: 12 additions & 2 deletions bfe_util/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ import (
"github.com/baidu/go-lib/log"
)

func AbnormalExit() {
func exit(code int) {
// waiting for logger finish jobs
log.Logger.Close()
// exit
os.Exit(1)
os.Exit(code)
}

// AbnormalExit abnormal status exit with code 1.
func AbnormalExit() {
exit(1)
}

// NormalExit normal status exit with code 0.
func NormalExit() {
exit(0)
}

0 comments on commit 8454396

Please sign in to comment.