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.

indexer.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 code
  5. import (
  6. "context"
  7. "os"
  8. "time"
  9. "code.gitea.io/gitea/modules/graceful"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. // SearchResult result of performing a search in a repo
  15. type SearchResult struct {
  16. RepoID int64
  17. StartIndex int
  18. EndIndex int
  19. Filename string
  20. Content string
  21. CommitID string
  22. UpdatedUnix timeutil.TimeStamp
  23. Language string
  24. Color string
  25. }
  26. // SearchResultLanguages result of top languages count in search results
  27. type SearchResultLanguages struct {
  28. Language string
  29. Color string
  30. Count int
  31. }
  32. // Indexer defines an interface to indexer issues contents
  33. type Indexer interface {
  34. Index(repoID int64) error
  35. Delete(repoID int64) error
  36. Search(repoIDs []int64, language, keyword string, page, pageSize int) (int64, []*SearchResult, []*SearchResultLanguages, error)
  37. Close()
  38. }
  39. // Init initialize the repo indexer
  40. func Init() {
  41. if !setting.Indexer.RepoIndexerEnabled {
  42. indexer.Close()
  43. return
  44. }
  45. initQueue(setting.Indexer.UpdateQueueLength)
  46. ctx, cancel := context.WithCancel(context.Background())
  47. graceful.GetManager().RunAtTerminate(ctx, func() {
  48. log.Debug("Closing repository indexer")
  49. indexer.Close()
  50. log.Info("PID: %d Repository Indexer closed", os.Getpid())
  51. })
  52. waitChannel := make(chan time.Duration)
  53. go func() {
  54. start := time.Now()
  55. log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
  56. bleveIndexer, created, err := NewBleveIndexer(setting.Indexer.RepoPath)
  57. if err != nil {
  58. if bleveIndexer != nil {
  59. bleveIndexer.Close()
  60. }
  61. cancel()
  62. indexer.Close()
  63. close(waitChannel)
  64. log.Fatal("PID: %d Unable to initialize the Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
  65. }
  66. indexer.set(bleveIndexer)
  67. go processRepoIndexerOperationQueue(indexer)
  68. if created {
  69. go populateRepoIndexer()
  70. }
  71. select {
  72. case waitChannel <- time.Since(start):
  73. case <-graceful.GetManager().IsShutdown():
  74. }
  75. close(waitChannel)
  76. }()
  77. if setting.Indexer.StartupTimeout > 0 {
  78. go func() {
  79. timeout := setting.Indexer.StartupTimeout
  80. if graceful.GetManager().IsChild() && setting.GracefulHammerTime > 0 {
  81. timeout += setting.GracefulHammerTime
  82. }
  83. select {
  84. case <-graceful.GetManager().IsShutdown():
  85. log.Warn("Shutdown before Repository Indexer completed initialization")
  86. cancel()
  87. indexer.Close()
  88. case duration, ok := <-waitChannel:
  89. if !ok {
  90. log.Warn("Repository Indexer Initialization failed")
  91. cancel()
  92. indexer.Close()
  93. return
  94. }
  95. log.Info("Repository Indexer Initialization took %v", duration)
  96. case <-time.After(timeout):
  97. cancel()
  98. indexer.Close()
  99. log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
  100. }
  101. }()
  102. }
  103. }