Skip to content

Commit 791b109

Browse files
zeripathlafriks
andauthored
Notification: queue ui.go notification-service (#10281)
Co-authored-by: Lauris BH <[email protected]>
1 parent 5563d56 commit 791b109

File tree

1 file changed

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

1 file changed

+27
-16
lines changed

modules/notification/ui/ui.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ package ui
66

77
import (
88
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/graceful"
910
"code.gitea.io/gitea/modules/log"
1011
"code.gitea.io/gitea/modules/notification/base"
12+
"code.gitea.io/gitea/modules/queue"
1113
)
1214

1315
type (
1416
notificationService struct {
1517
base.NullNotifier
16-
issueQueue chan issueNotificationOpts
18+
issueQueue queue.Queue
1719
}
1820

1921
issueNotificationOpts struct {
@@ -29,19 +31,24 @@ var (
2931

3032
// NewNotifier create a new notificationService notifier
3133
func NewNotifier() base.Notifier {
32-
return &notificationService{
33-
issueQueue: make(chan issueNotificationOpts, 100),
34-
}
34+
ns := &notificationService{}
35+
ns.issueQueue = queue.CreateQueue("notification-service", ns.handle, issueNotificationOpts{})
36+
return ns
3537
}
3638

37-
func (ns *notificationService) Run() {
38-
for opts := range ns.issueQueue {
39+
func (ns *notificationService) handle(data ...queue.Data) {
40+
for _, datum := range data {
41+
opts := datum.(issueNotificationOpts)
3942
if err := models.CreateOrUpdateIssueNotifications(opts.issueID, opts.commentID, opts.notificationAuthorID); err != nil {
4043
log.Error("Was unable to create issue notification: %v", err)
4144
}
4245
}
4346
}
4447

48+
func (ns *notificationService) Run() {
49+
graceful.GetManager().RunWithShutdownFns(ns.issueQueue.Run)
50+
}
51+
4552
func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
4653
issue *models.Issue, comment *models.Comment) {
4754
var opts = issueNotificationOpts{
@@ -51,35 +58,39 @@ func (ns *notificationService) NotifyCreateIssueComment(doer *models.User, repo
5158
if comment != nil {
5259
opts.commentID = comment.ID
5360
}
54-
ns.issueQueue <- opts
61+
_ = ns.issueQueue.Push(opts)
5562
}
5663

5764
func (ns *notificationService) NotifyNewIssue(issue *models.Issue) {
58-
ns.issueQueue <- issueNotificationOpts{
65+
_ = ns.issueQueue.Push(issueNotificationOpts{
5966
issueID: issue.ID,
6067
notificationAuthorID: issue.Poster.ID,
61-
}
68+
})
6269
}
6370

6471
func (ns *notificationService) NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, actionComment *models.Comment, isClosed bool) {
65-
ns.issueQueue <- issueNotificationOpts{
72+
_ = ns.issueQueue.Push(issueNotificationOpts{
6673
issueID: issue.ID,
6774
notificationAuthorID: doer.ID,
68-
}
75+
})
6976
}
7077

7178
func (ns *notificationService) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User) {
72-
ns.issueQueue <- issueNotificationOpts{
79+
_ = ns.issueQueue.Push(issueNotificationOpts{
7380
issueID: pr.Issue.ID,
7481
notificationAuthorID: doer.ID,
75-
}
82+
})
7683
}
7784

7885
func (ns *notificationService) NotifyNewPullRequest(pr *models.PullRequest) {
79-
ns.issueQueue <- issueNotificationOpts{
86+
if err := pr.LoadIssue(); err != nil {
87+
log.Error("Unable to load issue: %d for pr: %d: Error: %v", pr.IssueID, pr.ID, err)
88+
return
89+
}
90+
_ = ns.issueQueue.Push(issueNotificationOpts{
8091
issueID: pr.Issue.ID,
8192
notificationAuthorID: pr.Issue.PosterID,
82-
}
93+
})
8394
}
8495

8596
func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r *models.Review, c *models.Comment) {
@@ -90,5 +101,5 @@ func (ns *notificationService) NotifyPullRequestReview(pr *models.PullRequest, r
90101
if c != nil {
91102
opts.commentID = c.ID
92103
}
93-
ns.issueQueue <- opts
104+
_ = ns.issueQueue.Push(opts)
94105
}

0 commit comments

Comments
 (0)