Commit ffc11cb6 by Arve Knudsen Committed by GitHub

Server: Don't clutter trace.Stop by exiting (#27700)

* Server: Don't clutter trace.Stop by exiting

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent a9e49c24
package main
import (
"errors"
"flag"
"fmt"
"net/http"
......@@ -33,11 +34,21 @@ import (
_ "github.com/grafana/grafana/pkg/tsdb/testdatasource"
)
// The following variables cannot be constants, since they can be overridden through the -X link flag
var version = "5.0.0"
var commit = "NA"
var buildBranch = "master"
var buildstamp string
type exitWithCode struct {
reason string
code int
}
func (e exitWithCode) Error() string {
return e.reason
}
func main() {
var (
configFile = flag.String("config", "", "path to config file")
......@@ -82,6 +93,23 @@ func main() {
}()
}
if err := executeServer(*configFile, *homePath, *pidFile, *packaging, traceDiagnostics); err != nil {
code := 1
var ewc exitWithCode
if errors.As(err, &ewc) {
code = ewc.code
}
if code != 0 {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
os.Exit(code)
}
}
func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnostics *tracingDiagnostics) error {
defer log.Close()
if traceDiagnostics.enabled {
fmt.Println("diagnostics: tracing enabled", "file", traceDiagnostics.file)
f, err := os.Create(traceDiagnostics.file)
......@@ -90,15 +118,14 @@ func main() {
}
defer f.Close()
err = trace.Start(f)
if err != nil {
if err := trace.Start(f); err != nil {
panic(err)
}
defer trace.Stop()
}
buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
if buildstampInt64 == 0 {
buildstampInt64, err := strconv.ParseInt(buildstamp, 10, 64)
if err != nil || buildstampInt64 == 0 {
buildstampInt64 = time.Now().Unix()
}
......@@ -107,30 +134,29 @@ func main() {
setting.BuildStamp = buildstampInt64
setting.BuildBranch = buildBranch
setting.IsEnterprise = extensions.IsEnterprise
setting.Packaging = validPackaging(*packaging)
setting.Packaging = validPackaging(packaging)
metrics.SetBuildInformation(version, commit, buildBranch)
s, err := server.New(server.Config{
ConfigFile: *configFile, HomePath: *homePath, PidFile: *pidFile,
ConfigFile: configFile, HomePath: homePath, PidFile: pidFile,
Version: version, Commit: commit, BuildBranch: buildBranch,
})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
return err
}
go listenToSystemSignals(s)
err = s.Run()
code := 0
if err != nil {
code = s.ExitCode(err)
if err := s.Run(); err != nil {
code := s.ExitCode(err)
return exitWithCode{
reason: err.Error(),
code: code,
}
}
trace.Stop()
log.Close()
os.Exit(code)
return nil
}
func validPackaging(packaging string) string {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment