Skip to content

Commit 34609c9

Browse files
committed
Notification: queue ui.go notification-service
1 parent 811d549 commit 34609c9

File tree

1 file changed

+27
-15
lines changed
  • modules/notification/ui

1 file changed

+27
-15
lines changed

modules/notification/ui/ui.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ package ui
77
import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/git"
10+
"code.gitea.io/gitea/modules/graceful"
1011
"code.gitea.io/gitea/modules/log"
1112
"code.gitea.io/gitea/modules/notification/base"
13+
"code.gitea.io/gitea/modules/queue"
14+
"code.gitea.io/gitea/modules/setting"
1215
)
1316

1417
type (
1518
notificationService struct {
1619
base.NullNotifier
17-
issueQueue chan issueNotificationOpts
20+
issueQueue queue.Queue
1821
}
1922

2023
issueNotificationOpts struct {
@@ -30,19 +33,24 @@ var (
3033

3134
// NewNotifier create a new notificationService notifier
3235
func NewNotifier() base.Notifier {
33-
return &notificationService{
34-
issueQueue: make(chan issueNotificationOpts, 100),
35-
}
36+
ns := &notificationService{}
37+
ns.issueQueue = setting.CreateQueue("notification-service", ns.handle, issueNotificationOpts{})
38+
return ns
3639
}
3740

38-
func (ns *notificationService) Run() {
39-
for opts := range ns.issueQueue {
41+
func (ns *notificationService) handle(data ...queue.Data) {
42+
for _, datum := range data {
43+
opts := datum.(issueNotificationOpts)
4044
if err := models.CreateOrUpdateIssueNotifications(opts.issueID, opts.commentID, opts.notificationAuthorID); err != nil {
4145
log.Error("Was unable to create issue notification: %v", err)
4246
}
4347
}
4448
}
4549

50+
func (ns *notificationService) Run() {
51+
graceful.GetManager().RunWithShutdownFns(ns.issueQueue.Run)
52+
}
53+
4654
func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
4755
issue *models.Issue, comment *models.Comment) {
4856
var opts = issueNotificationOpts{
@@ -52,35 +60,39 @@ func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo
5260
if comment != nil {
5361
opts.commentID = comment.ID
5462
}
55-
ns.issueQueue <- opts
63+
_ = ns.issueQueue.Push(opts)
5664
}
5765

5866
func (ns *notificationService) NotifyNewIssue(issue *models.Issue) {
59-
ns.issueQueue <- issueNotificationOpts{
67+
_ = ns.issueQueue.Push(issueNotificationOpts{
6068
issueID: issue.ID,
6169
notificationAuthorID: issue.Poster.ID,
62-
}
70+
})
6371
}
6472

6573
func (ns *notificationService) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
6674
ns.issueQueue <- issueNotificationOpts{
6775
issueID: issue.ID,
6876
notificationAuthorID: doer.ID,
69-
}
77+
})
7078
}
7179

7280
func (ns *notificationService) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, gitRepo *git.Repository) {
73-
ns.issueQueue <- issueNotificationOpts{
81+
_ = ns.issueQueue.Push(issueNotificationOpts{
7482
issueID: pr.Issue.ID,
7583
notificationAuthorID: doer.ID,
76-
}
84+
})
7785
}
7886

7987
func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest) {
80-
ns.issueQueue <- issueNotificationOpts{
88+
if err := pr.LoadIssue(); err != nil {
89+
log.Error("Unable to load issue: %d for pr: %d: Error: %v", pr.IssueID, pr.ID, err)
90+
return
91+
}
92+
_ = ns.issueQueue.Push(issueNotificationOpts{
8193
issueID: pr.Issue.ID,
8294
notificationAuthorID: pr.Issue.PosterID,
83-
}
95+
})
8496
}
8597

8698
func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, c *models.Comment) {
@@ -91,5 +103,5 @@ func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r
91103
if c != nil {
92104
opts.commentID = c.ID
93105
}
94-
ns.issueQueue <- opts
106+
_ = ns.issueQueue.Push(opts)
95107
}

0 commit comments

Comments
 (0)