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.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2020 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 stats
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/graceful"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // Indexer defines an interface to index repository stats
  11. type Indexer interface {
  12. Index(id int64) error
  13. Close()
  14. }
  15. // indexer represents a indexer instance
  16. var indexer Indexer
  17. // Init initialize the repo indexer
  18. func Init() error {
  19. indexer = &DBIndexer{}
  20. if err := initStatsQueue(); err != nil {
  21. return err
  22. }
  23. go populateRepoIndexer()
  24. return nil
  25. }
  26. // populateRepoIndexer populate the repo indexer with pre-existing data. This
  27. // should only be run when the indexer is created for the first time.
  28. func populateRepoIndexer() {
  29. log.Info("Populating the repo stats indexer with existing repositories")
  30. isShutdown := graceful.GetManager().IsShutdown()
  31. exist, err := models.IsTableNotEmpty("repository")
  32. if err != nil {
  33. log.Fatal("System error: %v", err)
  34. } else if !exist {
  35. return
  36. }
  37. var maxRepoID int64
  38. if maxRepoID, err = models.GetMaxID("repository"); err != nil {
  39. log.Fatal("System error: %v", err)
  40. }
  41. // start with the maximum existing repo ID and work backwards, so that we
  42. // don't include repos that are created after gitea starts; such repos will
  43. // already be added to the indexer, and we don't need to add them again.
  44. for maxRepoID > 0 {
  45. select {
  46. case <-isShutdown:
  47. log.Info("Repository Stats Indexer population shutdown before completion")
  48. return
  49. default:
  50. }
  51. ids, err := models.GetUnindexedRepos(models.RepoIndexerTypeStats, maxRepoID, 0, 50)
  52. if err != nil {
  53. log.Error("populateRepoIndexer: %v", err)
  54. return
  55. } else if len(ids) == 0 {
  56. break
  57. }
  58. for _, id := range ids {
  59. select {
  60. case <-isShutdown:
  61. log.Info("Repository Stats Indexer population shutdown before completion")
  62. return
  63. default:
  64. }
  65. if err := statsQueue.Push(id); err != nil {
  66. log.Error("statsQueue.Push: %v", err)
  67. }
  68. maxRepoID = id - 1
  69. }
  70. }
  71. log.Info("Done (re)populating the repo stats indexer with existing repositories")
  72. }