Skip to content

Commit c0ea8ef

Browse files
committed
Graceful: Make TestPullRequests shutdownable
services/pull/check.go:TestPullRequests run runs within the shutdown context and will respond the manager requesting it be shutdown. In future this should probably become a persitable queue with pooled workers but that restructure can wait.
1 parent b2dea35 commit c0ea8ef

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

services/pull/check.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package pull
77

88
import (
9+
"context"
910
"fmt"
1011
"io/ioutil"
1112
"os"
@@ -16,6 +17,7 @@ import (
1617

1718
"code.gitea.io/gitea/models"
1819
"code.gitea.io/gitea/modules/git"
20+
"code.gitea.io/gitea/modules/graceful"
1921
"code.gitea.io/gitea/modules/log"
2022
"code.gitea.io/gitea/modules/setting"
2123
"code.gitea.io/gitea/modules/sync"
@@ -151,7 +153,7 @@ func manuallyMerged(pr *models.PullRequest) bool {
151153

152154
// TestPullRequests checks and tests untested patches of pull requests.
153155
// TODO: test more pull requests at same time.
154-
func TestPullRequests() {
156+
func TestPullRequests(ctx context.Context) {
155157
prs, err := models.GetPullRequestsByCheckStatus(models.PullRequestStatusChecking)
156158
if err != nil {
157159
log.Error("Find Checking PRs: %v", err)
@@ -176,34 +178,46 @@ func TestPullRequests() {
176178
}
177179

178180
checkAndUpdateStatus(pr)
181+
select {
182+
case <-ctx.Done():
183+
log.Info("PID: %d Pull Request testing shutdown", os.Getpid())
184+
return
185+
default:
186+
}
179187
}
180188

181189
// Start listening on new test requests.
182-
for prID := range pullRequestQueue.Queue() {
183-
log.Trace("TestPullRequests[%v]: processing test task", prID)
184-
pullRequestQueue.Remove(prID)
190+
for {
191+
select {
192+
case prID := <-pullRequestQueue.Queue():
193+
log.Trace("TestPullRequests[%v]: processing test task", prID)
194+
pullRequestQueue.Remove(prID)
195+
196+
id := com.StrTo(prID).MustInt64()
197+
if _, ok := checkedPRs[id]; ok {
198+
continue
199+
}
185200

186-
id := com.StrTo(prID).MustInt64()
187-
if _, ok := checkedPRs[id]; ok {
188-
continue
189-
}
201+
pr, err := models.GetPullRequestByID(id)
202+
if err != nil {
203+
log.Error("GetPullRequestByID[%s]: %v", prID, err)
204+
continue
205+
} else if manuallyMerged(pr) {
206+
continue
207+
} else if err = pr.TestPatch(); err != nil {
208+
log.Error("testPatch[%d]: %v", pr.ID, err)
209+
continue
210+
}
190211

191-
pr, err := models.GetPullRequestByID(id)
192-
if err != nil {
193-
log.Error("GetPullRequestByID[%s]: %v", prID, err)
194-
continue
195-
} else if manuallyMerged(pr) {
196-
continue
197-
} else if err = pr.TestPatch(); err != nil {
198-
log.Error("testPatch[%d]: %v", pr.ID, err)
199-
continue
212+
checkAndUpdateStatus(pr)
213+
case <-ctx.Done():
214+
log.Info("PID: %d Pull Request testing shutdown", os.Getpid())
215+
return
200216
}
201-
202-
checkAndUpdateStatus(pr)
203217
}
204218
}
205219

206220
// Init runs the task queue to test all the checking status pull requests
207221
func Init() {
208-
go TestPullRequests()
222+
go graceful.GetManager().RunWithShutdownContext(TestPullRequests)
209223
}

0 commit comments

Comments
 (0)