server.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "time"
  6. "golang.org/x/sync/errgroup"
  7. "github.com/grafana/grafana/pkg/api"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/login"
  10. "github.com/grafana/grafana/pkg/metrics"
  11. "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/plugins"
  13. "github.com/grafana/grafana/pkg/services/alerting"
  14. "github.com/grafana/grafana/pkg/services/cleanup"
  15. "github.com/grafana/grafana/pkg/services/eventpublisher"
  16. "github.com/grafana/grafana/pkg/services/notifications"
  17. "github.com/grafana/grafana/pkg/services/search"
  18. "github.com/grafana/grafana/pkg/setting"
  19. "github.com/grafana/grafana/pkg/social"
  20. )
  21. func NewGrafanaServer() models.GrafanaServer {
  22. rootCtx, shutdownFn := context.WithCancel(context.Background())
  23. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  24. return &GrafanaServerImpl{
  25. context: childCtx,
  26. shutdownFn: shutdownFn,
  27. childRoutines: childRoutines,
  28. log: log.New("server"),
  29. }
  30. }
  31. type GrafanaServerImpl struct {
  32. context context.Context
  33. shutdownFn context.CancelFunc
  34. childRoutines *errgroup.Group
  35. log log.Logger
  36. }
  37. func (g *GrafanaServerImpl) Start() {
  38. go listenToSystemSignals(g)
  39. writePIDFile()
  40. initRuntime()
  41. initSql()
  42. metrics.Init()
  43. search.Init()
  44. login.Init()
  45. social.NewOAuthService()
  46. eventpublisher.Init()
  47. plugins.Init()
  48. // init alerting
  49. if setting.AlertingEnabled && setting.ExecuteAlerts {
  50. engine := alerting.NewEngine()
  51. g.childRoutines.Go(func() error { return engine.Run(g.context) })
  52. }
  53. // cleanup service
  54. cleanUpService := cleanup.NewCleanUpService()
  55. g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
  56. if err := notifications.Init(); err != nil {
  57. g.log.Error("Notification service failed to initialize", "erro", err)
  58. g.Shutdown(1, "Startup failed")
  59. return
  60. }
  61. g.startHttpServer()
  62. }
  63. func (g *GrafanaServerImpl) startHttpServer() {
  64. httpServer := api.NewHttpServer()
  65. err := httpServer.Start(g.context)
  66. if err != nil {
  67. g.log.Error("Fail to start server", "error", err)
  68. g.Shutdown(1, "Startup failed")
  69. return
  70. }
  71. }
  72. func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
  73. g.log.Info("Shutdown started", "code", code, "reason", reason)
  74. g.shutdownFn()
  75. err := g.childRoutines.Wait()
  76. g.log.Info("Shutdown completed", "reason", err)
  77. log.Close()
  78. os.Exit(code)
  79. }
  80. // implement context.Context
  81. func (g *GrafanaServerImpl) Deadline() (deadline time.Time, ok bool) {
  82. return g.context.Deadline()
  83. }
  84. func (g *GrafanaServerImpl) Done() <-chan struct{} {
  85. return g.context.Done()
  86. }
  87. func (g *GrafanaServerImpl) Err() error {
  88. return g.context.Err()
  89. }
  90. func (g *GrafanaServerImpl) Value(key interface{}) interface{} {
  91. return g.context.Value(key)
  92. }