@@ -192,46 +192,85 @@ func AggregateJobStatus(jobs []*ActionRunJob) Status {
192192 }
193193}
194194
195- func ShouldBlockJobByConcurrency (ctx context.Context , actionRunJob * ActionRunJob ) (bool , error ) {
196- if len (actionRunJob .RawConcurrencyGroup ) == 0 {
195+ func ShouldBlockJobByConcurrency (ctx context.Context , job * ActionRunJob ) (bool , error ) {
196+ if len (job .RawConcurrencyGroup ) == 0 {
197197 return false , nil
198198 }
199- if ! actionRunJob .IsConcurrencyEvaluated {
199+ if ! job .IsConcurrencyEvaluated {
200200 return false , fmt .Errorf ("the raw concurrency group has not been evaluated" )
201201 }
202- if len (actionRunJob .ConcurrencyGroup ) == 0 {
202+ if len (job .ConcurrencyGroup ) == 0 || job . ConcurrencyCancel {
203203 return false , nil
204204 }
205- if actionRunJob .ConcurrencyCancel {
206- return false , CancelConcurrentJobs (ctx , actionRunJob )
207- }
208205
209206 concurrentJobsNum , err := db .Count [ActionRunJob ](ctx , FindRunJobOptions {
210- RepoID : actionRunJob .RepoID ,
211- ConcurrencyGroup : actionRunJob .ConcurrencyGroup ,
207+ RepoID : job .RepoID ,
208+ ConcurrencyGroup : job .ConcurrencyGroup ,
212209 Statuses : []Status {StatusRunning , StatusWaiting },
213210 })
214211 if err != nil {
215212 return false , fmt .Errorf ("count running and waiting jobs: %w" , err )
216213 }
214+ if concurrentJobsNum > 0 {
215+ return true , nil
216+ }
217+
218+ if err := job .LoadRun (ctx ); err != nil {
219+ return false , fmt .Errorf ("load run: %w" , err )
220+ }
217221
218- return concurrentJobsNum > 0 , nil
222+ return ShouldBlockRunByConcurrency ( ctx , job . Run )
219223}
220224
221- func CancelConcurrentJobs (ctx context.Context , actionRunJob * ActionRunJob ) error {
222- // cancel previous jobs in the same concurrency group
223- previousJobs , err := db .Find [ActionRunJob ](ctx , FindRunJobOptions {
224- RepoID : actionRunJob .RepoID ,
225- ConcurrencyGroup : actionRunJob .ConcurrencyGroup ,
226- Statuses : []Status {
227- StatusRunning ,
228- StatusWaiting ,
229- StatusBlocked ,
230- },
231- })
232- if err != nil {
233- return fmt .Errorf ("find previous jobs: %w" , err )
225+ func CancelPreviousJobsByConcurrency (ctx context.Context , job * ActionRunJob ) error {
226+ if len (job .RawConcurrencyGroup ) > 0 {
227+ if ! job .IsConcurrencyEvaluated {
228+ return fmt .Errorf ("the raw concurrency group has not been evaluated" )
229+ }
230+ if len (job .ConcurrencyGroup ) > 0 && job .ConcurrencyCancel {
231+ // cancel previous jobs in the same concurrency group
232+ previousJobs , err := db .Find [ActionRunJob ](ctx , & FindRunJobOptions {
233+ RepoID : job .RepoID ,
234+ ConcurrencyGroup : job .ConcurrencyGroup ,
235+ Statuses : []Status {StatusRunning , StatusWaiting , StatusBlocked },
236+ })
237+ if err != nil {
238+ return fmt .Errorf ("find previous jobs: %w" , err )
239+ }
240+ if err := CancelJobs (ctx , previousJobs ); err != nil {
241+ return fmt .Errorf ("cancel previous jobs: %w" , err )
242+ }
243+ }
234244 }
235245
236- return CancelJobs (ctx , previousJobs )
246+ if err := job .LoadRun (ctx ); err != nil {
247+ return fmt .Errorf ("load run: %w" , err )
248+ }
249+ if len (job .Run .ConcurrencyGroup ) > 0 && job .Run .ConcurrencyCancel {
250+ // cancel previous runs in the same concurrency group
251+ runs , err := db .Find [ActionRun ](ctx , & FindRunOptions {
252+ RepoID : job .RepoID ,
253+ ConcurrencyGroup : job .Run .ConcurrencyGroup ,
254+ Status : []Status {StatusRunning , StatusWaiting , StatusBlocked },
255+ })
256+ if err != nil {
257+ return fmt .Errorf ("find runs: %w" , err )
258+ }
259+ for _ , run := range runs {
260+ if run .ID == job .Run .ID {
261+ continue
262+ }
263+ jobs , err := db .Find [ActionRunJob ](ctx , FindRunJobOptions {
264+ RunID : run .ID ,
265+ })
266+ if err != nil {
267+ return fmt .Errorf ("find run %d jobs: %w" , run .ID , err )
268+ }
269+ if err := CancelJobs (ctx , jobs ); err != nil {
270+ return fmt .Errorf ("cancel run %d jobs: %w" , run .ID , err )
271+ }
272+ }
273+ }
274+
275+ return nil
237276}
0 commit comments