server.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "time"
  10. "golang.org/x/sync/errgroup"
  11. "github.com/grafana/grafana/pkg/api"
  12. "github.com/grafana/grafana/pkg/log"
  13. "github.com/grafana/grafana/pkg/login"
  14. "github.com/grafana/grafana/pkg/metrics"
  15. "github.com/grafana/grafana/pkg/models"
  16. "github.com/grafana/grafana/pkg/plugins"
  17. "github.com/grafana/grafana/pkg/services/alerting"
  18. "github.com/grafana/grafana/pkg/services/cleanup"
  19. "github.com/grafana/grafana/pkg/services/notifications"
  20. "github.com/grafana/grafana/pkg/services/search"
  21. "github.com/grafana/grafana/pkg/setting"
  22. "github.com/grafana/grafana/pkg/social"
  23. "github.com/grafana/grafana/pkg/tracing"
  24. )
  25. func NewGrafanaServer() models.GrafanaServer {
  26. rootCtx, shutdownFn := context.WithCancel(context.Background())
  27. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  28. return &GrafanaServerImpl{
  29. context: childCtx,
  30. shutdownFn: shutdownFn,
  31. childRoutines: childRoutines,
  32. log: log.New("server"),
  33. }
  34. }
  35. type GrafanaServerImpl struct {
  36. context context.Context
  37. shutdownFn context.CancelFunc
  38. childRoutines *errgroup.Group
  39. log log.Logger
  40. httpServer *api.HttpServer
  41. }
  42. func (g *GrafanaServerImpl) Start() {
  43. go listenToSystemSignals(g)
  44. g.initLogging()
  45. g.writePIDFile()
  46. initSql()
  47. metrics.Init(setting.Cfg)
  48. search.Init()
  49. login.Init()
  50. social.NewOAuthService()
  51. plugins.Init()
  52. closer, err := tracing.Init(setting.Cfg)
  53. if err != nil {
  54. g.log.Error("Tracing settings is not valid", "error", err)
  55. g.Shutdown(1, "Startup failed")
  56. return
  57. }
  58. defer closer.Close()
  59. // init alerting
  60. if setting.AlertingEnabled && setting.ExecuteAlerts {
  61. engine := alerting.NewEngine()
  62. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  63. }
  64. // cleanup service
  65. cleanUpService := cleanup.NewCleanUpService()
  66. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  67. if err = notifications.Init(); err != nil {
  68. g.log.Error("Notification service failed to initialize", "error", err)
  69. g.Shutdown(1, "Startup failed")
  70. return
  71. }
  72. g.startHttpServer()
  73. }
  74. func (g *GrafanaServerImpl) initLogging() {
  75. err := setting.NewConfigContext(&setting.CommandLineArgs{
  76. Config: *configFile,
  77. HomePath: *homePath,
  78. Args: flag.Args(),
  79. })
  80. if err != nil {
  81. g.log.Error(err.Error())
  82. os.Exit(1)
  83. }
  84. g.log.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  85. setting.LogConfigurationInfo()
  86. }
  87. func (g *GrafanaServerImpl) startHttpServer() {
  88. g.httpServer = api.NewHttpServer()
  89. err := g.httpServer.Start(g.context)
  90. if err != nil {
  91. g.log.Error("Fail to start server", "error", err)
  92. g.Shutdown(1, "Startup failed")
  93. return
  94. }
  95. }
  96. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  97. g.log.Info("Shutdown started", "code", code, "reason", reason)
  98. err := g.httpServer.Shutdown(g.context)
  99. if err != nil {
  100. g.log.Error("Failed to shutdown server", "error", err)
  101. }
  102. g.shutdownFn()
  103. err = g.childRoutines.Wait()
  104. g.log.Info("Shutdown completed", "reason", err)
  105. log.Close()
  106. os.Exit(code)
  107. }
  108. func (g *GrafanaServerImpl) writePIDFile() {
  109. if *pidFile == "" {
  110. return
  111. }
  112. // Ensure the required directory structure exists.
  113. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  114. if err != nil {
  115. g.log.Error("Failed to verify pid directory", "error", err)
  116. os.Exit(1)
  117. }
  118. // Retrieve the PID and write it.
  119. pid := strconv.Itoa(os.Getpid())
  120. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  121. g.log.Error("Failed to write pidfile", "error", err)
  122. os.Exit(1)
  123. }
  124. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  125. }