Skip to content

Commit 7beda2d

Browse files
committed
Graceful: AddTestPullRequest run in graceful ctx
1 parent c0ea8ef commit 7beda2d

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

services/pull/pull.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package pull
66

77
import (
8+
"context"
89
"fmt"
910

1011
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/graceful"
1214
"code.gitea.io/gitea/modules/log"
1315
"code.gitea.io/gitea/modules/notification"
1416
issue_service "code.gitea.io/gitea/services/issue"
@@ -44,6 +46,7 @@ func checkForInvalidation(requests models.PullRequestList, repoID int64, doer *m
4446
return fmt.Errorf("git.OpenRepository: %v", err)
4547
}
4648
go func() {
49+
// FIXME: graceful: We need to tell the manager we're doing something...
4750
err := requests.InvalidateCodeComments(doer, gitRepo, branch)
4851
if err != nil {
4952
log.Error("PullRequestList.InvalidateCodeComments: %v", err)
@@ -72,37 +75,43 @@ func addHeadRepoTasks(prs []*models.PullRequest) {
7275
// and generate new patch for testing as needed.
7376
func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSync bool) {
7477
log.Trace("AddTestPullRequestTask [head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch)
75-
prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
76-
if err != nil {
77-
log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
78-
return
79-
}
78+
graceful.GetManager().RunWithShutdownContext(func(ctx context.Context) {
79+
// There is no sensible way to shut this down ":-("
80+
// If you don't let it run all the way then you will lose data
81+
// FIXME: graceful: AddTestPullRequestTask needs to become a queue!
8082

81-
if isSync {
82-
requests := models.PullRequestList(prs)
83-
if err = requests.LoadAttributes(); err != nil {
84-
log.Error("PullRequestList.LoadAttributes: %v", err)
85-
}
86-
if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
87-
log.Error("checkForInvalidation: %v", invalidationErr)
83+
prs, err := models.GetUnmergedPullRequestsByHeadInfo(repoID, branch)
84+
if err != nil {
85+
log.Error("Find pull requests [head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err)
86+
return
8887
}
89-
if err == nil {
90-
for _, pr := range prs {
91-
pr.Issue.PullRequest = pr
92-
notification.NotifyPullRequestSynchronized(doer, pr)
88+
89+
if isSync {
90+
requests := models.PullRequestList(prs)
91+
if err = requests.LoadAttributes(); err != nil {
92+
log.Error("PullRequestList.LoadAttributes: %v", err)
93+
}
94+
if invalidationErr := checkForInvalidation(requests, repoID, doer, branch); invalidationErr != nil {
95+
log.Error("checkForInvalidation: %v", invalidationErr)
96+
}
97+
if err == nil {
98+
for _, pr := range prs {
99+
pr.Issue.PullRequest = pr
100+
notification.NotifyPullRequestSynchronized(doer, pr)
101+
}
93102
}
94103
}
95-
}
96104

97-
addHeadRepoTasks(prs)
105+
addHeadRepoTasks(prs)
98106

99-
log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
100-
prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
101-
if err != nil {
102-
log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
103-
return
104-
}
105-
for _, pr := range prs {
106-
AddToTaskQueue(pr)
107-
}
107+
log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
108+
prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
109+
if err != nil {
110+
log.Error("Find pull requests [base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err)
111+
return
112+
}
113+
for _, pr := range prs {
114+
AddToTaskQueue(pr)
115+
}
116+
})
108117
}

0 commit comments

Comments
 (0)