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 3.9KB

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