Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_indexer.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2017 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 models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/indexer"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/util"
  11. )
  12. // issueIndexerUpdateQueue queue of issue ids to be updated
  13. var issueIndexerUpdateQueue chan int64
  14. // InitIssueIndexer initialize issue indexer
  15. func InitIssueIndexer() {
  16. indexer.InitIssueIndexer(populateIssueIndexer)
  17. issueIndexerUpdateQueue = make(chan int64, setting.Indexer.UpdateQueueLength)
  18. go processIssueIndexerUpdateQueue()
  19. }
  20. // populateIssueIndexer populate the issue indexer with issue data
  21. func populateIssueIndexer() error {
  22. batch := indexer.IssueIndexerBatch()
  23. for page := 1; ; page++ {
  24. repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
  25. Page: page,
  26. PageSize: 10,
  27. OrderBy: SearchOrderByID,
  28. Private: true,
  29. Collaborate: util.OptionalBoolFalse,
  30. })
  31. if err != nil {
  32. return fmt.Errorf("Repositories: %v", err)
  33. }
  34. if len(repos) == 0 {
  35. return batch.Flush()
  36. }
  37. for _, repo := range repos {
  38. issues, err := Issues(&IssuesOptions{
  39. RepoID: repo.ID,
  40. IsClosed: util.OptionalBoolNone,
  41. IsPull: util.OptionalBoolNone,
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. for _, issue := range issues {
  47. if err := batch.Add(issue.update()); err != nil {
  48. return err
  49. }
  50. }
  51. }
  52. }
  53. }
  54. func processIssueIndexerUpdateQueue() {
  55. batch := indexer.IssueIndexerBatch()
  56. for {
  57. var issueID int64
  58. select {
  59. case issueID = <-issueIndexerUpdateQueue:
  60. default:
  61. // flush whatever updates we currently have, since we
  62. // might have to wait a while
  63. if err := batch.Flush(); err != nil {
  64. log.Error(4, "IssueIndexer: %v", err)
  65. }
  66. issueID = <-issueIndexerUpdateQueue
  67. }
  68. issue, err := GetIssueByID(issueID)
  69. if err != nil {
  70. log.Error(4, "GetIssueByID: %v", err)
  71. } else if err = batch.Add(issue.update()); err != nil {
  72. log.Error(4, "IssueIndexer: %v", err)
  73. }
  74. }
  75. }
  76. func (issue *Issue) update() indexer.IssueIndexerUpdate {
  77. comments := make([]string, 0, 5)
  78. for _, comment := range issue.Comments {
  79. if comment.Type == CommentTypeComment {
  80. comments = append(comments, comment.Content)
  81. }
  82. }
  83. return indexer.IssueIndexerUpdate{
  84. IssueID: issue.ID,
  85. Data: &indexer.IssueIndexerData{
  86. RepoID: issue.RepoID,
  87. Title: issue.Title,
  88. Content: issue.Content,
  89. Comments: comments,
  90. },
  91. }
  92. }
  93. // UpdateIssueIndexer add/update an issue to the issue indexer
  94. func UpdateIssueIndexer(issueID int64) {
  95. select {
  96. case issueIndexerUpdateQueue <- issueID:
  97. default:
  98. go func() {
  99. issueIndexerUpdateQueue <- issueID
  100. }()
  101. }
  102. }