@@ -47,6 +47,7 @@ type ActionRun struct {
4747 Status Status `xorm:"index"`
4848 Version int `xorm:"version default 0"` // Status could be updated concomitantly, so an optimistic lock is needed
4949 ConcurrencyGroup string
50+ ConcurrencyCancel bool
5051 // Started and Stopped is used for recording last run time, if rerun happened, they will be reset to 0
5152 Started timeutil.TimeStamp
5253 Stopped timeutil.TimeStamp
@@ -270,7 +271,7 @@ func CancelPreviousJobsWithOpts(ctx context.Context, opts *FindRunOptions) error
270271
271272// InsertRun inserts a run
272273// The title will be cut off at 255 characters if it's longer than 255 characters.
273- func InsertRun (ctx context.Context , run * ActionRun , jobs []* jobparser.SingleWorkflow , blockedByConcurrency bool ) error {
274+ func InsertRun (ctx context.Context , run * ActionRun , jobs []* jobparser.SingleWorkflow ) error {
274275 ctx , committer , err := db .TxContext (ctx )
275276 if err != nil {
276277 return err
@@ -284,6 +285,32 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
284285 run .Index = index
285286 run .Title , _ = util .SplitStringAtByteN (run .Title , 255 )
286287
288+ blockedByWorkflowConcurrency := false
289+ if len (run .ConcurrencyGroup ) > 0 {
290+ if run .ConcurrencyCancel {
291+ if err := CancelPreviousJobsWithOpts (ctx , & FindRunOptions {
292+ RepoID : run .RepoID ,
293+ ConcurrencyGroup : run .ConcurrencyGroup ,
294+ Status : []Status {StatusRunning , StatusWaiting , StatusBlocked },
295+ }); err != nil {
296+ return err
297+ }
298+ } else {
299+ waitingConcurrentRunsNum , err := db .Count [ActionRun ](ctx , & FindRunOptions {
300+ RepoID : run .RepoID ,
301+ ConcurrencyGroup : run .ConcurrencyGroup ,
302+ Status : []Status {StatusWaiting },
303+ })
304+ if err != nil {
305+ return err
306+ }
307+ blockedByWorkflowConcurrency = waitingConcurrentRunsNum > 0
308+ }
309+ }
310+ if blockedByWorkflowConcurrency {
311+ run .Status = StatusBlocked
312+ }
313+
287314 if err := db .Insert (ctx , run ); err != nil {
288315 return err
289316 }
@@ -310,13 +337,13 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
310337 }
311338 payload , _ := v .Marshal ()
312339 status := StatusWaiting
313- if len (needs ) > 0 || run .NeedApproval {
340+ if len (needs ) > 0 || run .NeedApproval || blockedByWorkflowConcurrency {
314341 status = StatusBlocked
315342 } else {
316343 hasWaiting = true
317344 }
318345 job .Name , _ = util .SplitStringAtByteN (job .Name , 255 )
319- runJobs = append ( runJobs , & ActionRunJob {
346+ runJob := & ActionRunJob {
320347 RunID : run .ID ,
321348 RepoID : run .RepoID ,
322349 OwnerID : run .OwnerID ,
@@ -328,7 +355,12 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
328355 Needs : needs ,
329356 RunsOn : job .RunsOn (),
330357 Status : status ,
331- })
358+ }
359+ if job .RawConcurrency != nil {
360+ runJob .RawConcurrencyGroup = job .RawConcurrency .Group
361+ runJob .RawConcurrencyCancel = job .RawConcurrency .CancelInProgress
362+ }
363+ runJobs = append (runJobs , runJob )
332364 }
333365 if err := db .Insert (ctx , runJobs ); err != nil {
334366 return err
0 commit comments