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

issue_indexer.go 2.4KB

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