server.go 6.7 KB

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