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

indexer.go 2.7 kB

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