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.

issue_indexer.go 2.7 kB

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