You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package routers
  5. import (
  6. "context"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/migrations"
  12. "code.gitea.io/gitea/modules/auth/sso"
  13. "code.gitea.io/gitea/modules/cache"
  14. "code.gitea.io/gitea/modules/cron"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/highlight"
  17. code_indexer "code.gitea.io/gitea/modules/indexer/code"
  18. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/markup"
  21. "code.gitea.io/gitea/modules/markup/external"
  22. "code.gitea.io/gitea/modules/notification"
  23. "code.gitea.io/gitea/modules/setting"
  24. "code.gitea.io/gitea/modules/ssh"
  25. "code.gitea.io/gitea/modules/task"
  26. "code.gitea.io/gitea/modules/webhook"
  27. "code.gitea.io/gitea/services/mailer"
  28. mirror_service "code.gitea.io/gitea/services/mirror"
  29. pull_service "code.gitea.io/gitea/services/pull"
  30. "gitea.com/macaron/macaron"
  31. )
  32. func checkRunMode() {
  33. switch setting.Cfg.Section("").Key("RUN_MODE").String() {
  34. case "prod":
  35. macaron.Env = macaron.PROD
  36. macaron.ColorLog = false
  37. setting.ProdMode = true
  38. default:
  39. git.Debug = true
  40. }
  41. log.Info("Run Mode: %s", strings.Title(macaron.Env))
  42. }
  43. // NewServices init new services
  44. func NewServices() {
  45. setting.NewServices()
  46. mailer.NewContext()
  47. _ = cache.NewContext()
  48. notification.NewContext()
  49. }
  50. // In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
  51. func initDBEngine(ctx context.Context) (err error) {
  52. log.Info("Beginning ORM engine initialization.")
  53. for i := 0; i < setting.Database.DBConnectRetries; i++ {
  54. select {
  55. case <-ctx.Done():
  56. return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization")
  57. default:
  58. }
  59. log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
  60. if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
  61. break
  62. } else if i == setting.Database.DBConnectRetries-1 {
  63. return err
  64. }
  65. log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
  66. log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
  67. time.Sleep(setting.Database.DBConnectBackoff)
  68. }
  69. models.HasEngine = true
  70. return nil
  71. }
  72. // GlobalInit is for global configuration reload-able.
  73. func GlobalInit(ctx context.Context) {
  74. setting.NewContext()
  75. if err := git.Init(ctx); err != nil {
  76. log.Fatal("Git module init failed: %v", err)
  77. }
  78. setting.CheckLFSVersion()
  79. log.Trace("AppPath: %s", setting.AppPath)
  80. log.Trace("AppWorkPath: %s", setting.AppWorkPath)
  81. log.Trace("Custom path: %s", setting.CustomPath)
  82. log.Trace("Log path: %s", setting.LogRootPath)
  83. NewServices()
  84. if setting.InstallLock {
  85. highlight.NewContext()
  86. external.RegisterParsers()
  87. markup.Init()
  88. if err := initDBEngine(ctx); err == nil {
  89. log.Info("ORM engine initialization successful!")
  90. } else {
  91. log.Fatal("ORM engine initialization failed: %v", err)
  92. }
  93. if err := models.InitOAuth2(); err != nil {
  94. log.Fatal("Failed to initialize OAuth2 support: %v", err)
  95. }
  96. models.NewRepoContext()
  97. // Booting long running goroutines.
  98. cron.NewContext()
  99. issue_indexer.InitIssueIndexer(false)
  100. code_indexer.Init()
  101. mirror_service.InitSyncMirrors()
  102. webhook.InitDeliverHooks()
  103. pull_service.Init()
  104. if err := task.Init(); err != nil {
  105. log.Fatal("Failed to initialize task scheduler: %v", err)
  106. }
  107. }
  108. if setting.EnableSQLite3 {
  109. log.Info("SQLite3 Supported")
  110. }
  111. checkRunMode()
  112. // Now because Install will re-run GlobalInit once it has set InstallLock
  113. // we can't tell if the ssh port will remain unused until that's done.
  114. // However, see FIXME comment in install.go
  115. if setting.InstallLock {
  116. if setting.SSH.StartBuiltinServer {
  117. ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  118. log.Info("SSH server started on %s:%d. Cipher list (%v), key exchange algorithms (%v), MACs (%v)", setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  119. } else {
  120. ssh.Unused()
  121. }
  122. }
  123. if setting.InstallLock {
  124. sso.Init()
  125. }
  126. }