server.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. )
  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. metricsCfg := metrics.ReadSettings(setting.Cfg)
  48. metrics.Init(metricsCfg)
  49. search.Init()
  50. login.Init()
  51. social.NewOAuthService()
  52. eventpublisher.Init()
  53. plugins.Init()
  54. // init alerting
  55. if setting.AlertingEnabled && setting.ExecuteAlerts {
  56. engine := alerting.NewEngine()
  57. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  58. }
  59. // cleanup service
  60. cleanUpService := cleanup.NewCleanUpService()
  61. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  62. if err := notifications.Init(); err != nil {
  63. g.log.Error("Notification service failed to initialize", "erro", err)
  64. g.Shutdown(1, "Startup failed")
  65. return
  66. }
  67. g.startHttpServer()
  68. }
  69. func (g *GrafanaServerImpl) initLogging() {
  70. err := setting.NewConfigContext(&setting.CommandLineArgs{
  71. Config: *configFile,
  72. HomePath: *homePath,
  73. Args: flag.Args(),
  74. })
  75. if err != nil {
  76. g.log.Error(err.Error())
  77. os.Exit(1)
  78. }
  79. g.log.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  80. setting.LogConfigurationInfo()
  81. }
  82. func (g *GrafanaServerImpl) startHttpServer() {
  83. g.httpServer = api.NewHttpServer()
  84. err := g.httpServer.Start(g.context)
  85. if err != nil {
  86. g.log.Error("Fail to start server", "error", err)
  87. g.Shutdown(1, "Startup failed")
  88. return
  89. }
  90. }
  91. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  92. g.log.Info("Shutdown started", "code", code, "reason", reason)
  93. err := g.httpServer.Shutdown(g.context)
  94. if err != nil {
  95. g.log.Error("Failed to shutdown server", "error", err)
  96. }
  97. g.shutdownFn()
  98. err = g.childRoutines.Wait()
  99. g.log.Info("Shutdown completed", "reason", err)
  100. log.Close()
  101. os.Exit(code)
  102. }
  103. func (g *GrafanaServerImpl) writePIDFile() {
  104. if *pidFile == "" {
  105. return
  106. }
  107. // Ensure the required directory structure exists.
  108. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  109. if err != nil {
  110. g.log.Error("Failed to verify pid directory", "error", err)
  111. os.Exit(1)
  112. }
  113. // Retrieve the PID and write it.
  114. pid := strconv.Itoa(os.Getpid())
  115. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  116. g.log.Error("Failed to write pidfile", "error", err)
  117. os.Exit(1)
  118. }
  119. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  120. }