server.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "path/filepath"
  10. "reflect"
  11. "strconv"
  12. "time"
  13. "github.com/facebookgo/inject"
  14. "github.com/grafana/grafana/pkg/bus"
  15. "github.com/grafana/grafana/pkg/middleware"
  16. "github.com/grafana/grafana/pkg/registry"
  17. "github.com/grafana/grafana/pkg/services/dashboards"
  18. "golang.org/x/sync/errgroup"
  19. "github.com/grafana/grafana/pkg/api"
  20. "github.com/grafana/grafana/pkg/log"
  21. "github.com/grafana/grafana/pkg/login"
  22. "github.com/grafana/grafana/pkg/services/sqlstore"
  23. "github.com/grafana/grafana/pkg/setting"
  24. "github.com/grafana/grafana/pkg/social"
  25. "github.com/grafana/grafana/pkg/tracing"
  26. // self registering services
  27. _ "github.com/grafana/grafana/pkg/extensions"
  28. _ "github.com/grafana/grafana/pkg/metrics"
  29. _ "github.com/grafana/grafana/pkg/plugins"
  30. _ "github.com/grafana/grafana/pkg/services/alerting"
  31. _ "github.com/grafana/grafana/pkg/services/cleanup"
  32. _ "github.com/grafana/grafana/pkg/services/notifications"
  33. _ "github.com/grafana/grafana/pkg/services/provisioning"
  34. _ "github.com/grafana/grafana/pkg/services/search"
  35. )
  36. func NewGrafanaServer() *GrafanaServerImpl {
  37. rootCtx, shutdownFn := context.WithCancel(context.Background())
  38. childRoutines, childCtx := errgroup.WithContext(rootCtx)
  39. return &GrafanaServerImpl{
  40. context: childCtx,
  41. shutdownFn: shutdownFn,
  42. childRoutines: childRoutines,
  43. log: log.New("server"),
  44. cfg: setting.NewCfg(),
  45. }
  46. }
  47. type GrafanaServerImpl struct {
  48. context context.Context
  49. shutdownFn context.CancelFunc
  50. childRoutines *errgroup.Group
  51. log log.Logger
  52. cfg *setting.Cfg
  53. shutdownReason string
  54. shutdownInProgress bool
  55. RouteRegister api.RouteRegister `inject:""`
  56. HttpServer *api.HTTPServer `inject:""`
  57. }
  58. func (g *GrafanaServerImpl) Run() error {
  59. g.loadConfiguration()
  60. g.writePIDFile()
  61. // initSql
  62. sqlstore.NewEngine() // TODO: this should return an error
  63. sqlstore.EnsureAdminUser()
  64. login.Init()
  65. social.NewOAuthService()
  66. tracingCloser, err := tracing.Init(g.cfg.Raw)
  67. if err != nil {
  68. return fmt.Errorf("Tracing settings is not valid. error: %v", err)
  69. }
  70. defer tracingCloser.Close()
  71. serviceGraph := inject.Graph{}
  72. serviceGraph.Provide(&inject.Object{Value: bus.GetBus()})
  73. serviceGraph.Provide(&inject.Object{Value: g.cfg})
  74. serviceGraph.Provide(&inject.Object{Value: dashboards.NewProvisioningService()})
  75. serviceGraph.Provide(&inject.Object{Value: api.NewRouteRegister(middleware.RequestMetrics, middleware.RequestTracing)})
  76. // self registered services
  77. services := registry.GetServices()
  78. // Add all services to dependency graph
  79. for _, service := range services {
  80. serviceGraph.Provide(&inject.Object{Value: service})
  81. }
  82. serviceGraph.Provide(&inject.Object{Value: g})
  83. // Inject dependencies to services
  84. if err := serviceGraph.Populate(); err != nil {
  85. return fmt.Errorf("Failed to populate service dependency: %v", err)
  86. }
  87. // Init & start services
  88. for _, service := range services {
  89. if registry.IsDisabled(service) {
  90. continue
  91. }
  92. g.log.Info("Initializing " + reflect.TypeOf(service).Elem().Name())
  93. if err := service.Init(); err != nil {
  94. return fmt.Errorf("Service init failed: %v", err)
  95. }
  96. }
  97. // Start background services
  98. for index := range services {
  99. service, ok := services[index].(registry.BackgroundService)
  100. if !ok {
  101. continue
  102. }
  103. if registry.IsDisabled(services[index]) {
  104. continue
  105. }
  106. g.childRoutines.Go(func() error {
  107. // Skip starting new service when shutting down
  108. // Can happen when service stop/return during startup
  109. if g.shutdownInProgress {
  110. return nil
  111. }
  112. err := service.Run(g.context)
  113. // If error is not canceled then the service crashed
  114. if err != context.Canceled {
  115. g.log.Error("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
  116. } else {
  117. g.log.Info("Stopped "+reflect.TypeOf(service).Elem().Name(), "reason", err)
  118. }
  119. // Mark that we are in shutdown mode
  120. // So more services are not started
  121. g.shutdownInProgress = true
  122. return err
  123. })
  124. }
  125. sendSystemdNotification("READY=1")
  126. return g.childRoutines.Wait()
  127. }
  128. func (g *GrafanaServerImpl) loadConfiguration() {
  129. err := g.cfg.Load(&setting.CommandLineArgs{
  130. Config: *configFile,
  131. HomePath: *homePath,
  132. Args: flag.Args(),
  133. })
  134. if err != nil {
  135. fmt.Fprintf(os.Stderr, "Failed to start grafana. error: %s\n", err.Error())
  136. os.Exit(1)
  137. }
  138. g.log.Info("Starting "+setting.ApplicationName, "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  139. g.cfg.LogConfigSources()
  140. }
  141. func (g *GrafanaServerImpl) Shutdown(reason string) {
  142. g.log.Info("Shutdown started", "reason", reason)
  143. g.shutdownReason = reason
  144. g.shutdownInProgress = true
  145. // call cancel func on root context
  146. g.shutdownFn()
  147. // wait for child routines
  148. g.childRoutines.Wait()
  149. }
  150. func (g *GrafanaServerImpl) Exit(reason error) {
  151. // default exit code is 1
  152. code := 1
  153. if reason == context.Canceled && g.shutdownReason != "" {
  154. reason = fmt.Errorf(g.shutdownReason)
  155. code = 0
  156. }
  157. g.log.Error("Server shutdown", "reason", reason)
  158. os.Exit(code)
  159. }
  160. func (g *GrafanaServerImpl) writePIDFile() {
  161. if *pidFile == "" {
  162. return
  163. }
  164. // Ensure the required directory structure exists.
  165. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  166. if err != nil {
  167. g.log.Error("Failed to verify pid directory", "error", err)
  168. os.Exit(1)
  169. }
  170. // Retrieve the PID and write it.
  171. pid := strconv.Itoa(os.Getpid())
  172. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  173. g.log.Error("Failed to write pidfile", "error", err)
  174. os.Exit(1)
  175. }
  176. g.log.Info("Writing PID file", "path", *pidFile, "pid", pid)
  177. }
  178. func sendSystemdNotification(state string) error {
  179. notifySocket := os.Getenv("NOTIFY_SOCKET")
  180. if notifySocket == "" {
  181. return fmt.Errorf("NOTIFY_SOCKET environment variable empty or unset.")
  182. }
  183. socketAddr := &net.UnixAddr{
  184. Name: notifySocket,
  185. Net: "unixgram",
  186. }
  187. conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
  188. if err != nil {
  189. return err
  190. }
  191. _, err = conn.Write([]byte(state))
  192. conn.Close()
  193. return err
  194. }