server.go 4.1 KB

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