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

issue_indexer.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. })
  30. if err != nil {
  31. return fmt.Errorf("Repositories: %v", err)
  32. }
  33. if len(repos) == 0 {
  34. return batch.Flush()
  35. }
  36. for _, repo := range repos {
  37. issues, err := Issues(&IssuesOptions{
  38. RepoID: repo.ID,
  39. IsClosed: util.OptionalBoolNone,
  40. IsPull: util.OptionalBoolNone,
  41. })
  42. if err != nil {
  43. return err
  44. }
  45. for _, issue := range issues {
  46. if err := batch.Add(issue.update()); err != nil {
  47. return err
  48. }
  49. }
  50. }
  51. }
  52. }
  53. func processIssueIndexerUpdateQueue() {
  54. batch := indexer.IssueIndexerBatch()
  55. for {
  56. var issueID int64
  57. select {
  58. case issueID = <-issueIndexerUpdateQueue:
  59. default:
  60. // flush whatever updates we currently have, since we
  61. // might have to wait a while
  62. if err := batch.Flush(); err != nil {
  63. log.Error(4, "IssueIndexer: %v", err)
  64. }
  65. issueID = <-issueIndexerUpdateQueue
  66. }
  67. issue, err := GetIssueByID(issueID)
  68. if err != nil {
  69. log.Error(4, "GetIssueByID: %v", err)
  70. } else if err = batch.Add(issue.update()); err != nil {
  71. log.Error(4, "IssueIndexer: %v", err)
  72. }
  73. }
  74. }
  75. func (issue *Issue) update() indexer.IssueIndexerUpdate {
  76. comments := make([]string, 0, 5)
  77. for _, comment := range issue.Comments {
  78. if comment.Type == CommentTypeComment {
  79. comments = append(comments, comment.Content)
  80. }
  81. }
  82. return indexer.IssueIndexerUpdate{
  83. IssueID: issue.ID,
  84. Data: &indexer.IssueIndexerData{
  85. RepoID: issue.RepoID,
  86. Title: issue.Title,
  87. Content: issue.Content,
  88. Comments: comments,
  89. },
  90. }
  91. }
  92. // UpdateIssueIndexer add/update an issue to the issue indexer
  93. func UpdateIssueIndexer(issueID int64) {
  94. select {
  95. case issueIndexerUpdateQueue <- issueID:
  96. default:
  97. go func() {
  98. issueIndexerUpdateQueue <- issueID
  99. }()
  100. }
  101. }