server.go 3.4 KB

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