6
6
package pull
7
7
8
8
import (
9
+ "context"
9
10
"fmt"
10
11
"io/ioutil"
11
12
"os"
@@ -16,6 +17,7 @@ import (
16
17
17
18
"code.gitea.io/gitea/models"
18
19
"code.gitea.io/gitea/modules/git"
20
+ "code.gitea.io/gitea/modules/graceful"
19
21
"code.gitea.io/gitea/modules/log"
20
22
"code.gitea.io/gitea/modules/setting"
21
23
"code.gitea.io/gitea/modules/sync"
@@ -151,7 +153,7 @@ func manuallyMerged(pr *models.PullRequest) bool {
151
153
152
154
// TestPullRequests checks and tests untested patches of pull requests.
153
155
// TODO: test more pull requests at same time.
154
- func TestPullRequests () {
156
+ func TestPullRequests (ctx context. Context ) {
155
157
prs , err := models .GetPullRequestsByCheckStatus (models .PullRequestStatusChecking )
156
158
if err != nil {
157
159
log .Error ("Find Checking PRs: %v" , err )
@@ -176,34 +178,46 @@ func TestPullRequests() {
176
178
}
177
179
178
180
checkAndUpdateStatus (pr )
181
+ select {
182
+ case <- ctx .Done ():
183
+ log .Info ("PID: %d Pull Request testing shutdown" , os .Getpid ())
184
+ return
185
+ default :
186
+ }
179
187
}
180
188
181
189
// 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
+ }
185
200
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
+ }
190
211
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
200
216
}
201
-
202
- checkAndUpdateStatus (pr )
203
217
}
204
218
}
205
219
206
220
// Init runs the task queue to test all the checking status pull requests
207
221
func Init () {
208
- go TestPullRequests ( )
222
+ go graceful . GetManager (). RunWithShutdownContext ( TestPullRequests )
209
223
}
0 commit comments