server.go 3.7 KB

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