@@ -61,13 +61,28 @@ func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) er
6161 return err
6262}
6363
64- // PrepareToStartJobWithConcurrency prepares a job to start by evaluating its concurrency group and cancelling previous jobs if necessary.
64+ func shouldBlockJobByConcurrency (ctx context.Context , job * actions_model.ActionRunJob ) (bool , error ) {
65+ if job .RawConcurrency != "" && ! job .IsConcurrencyEvaluated {
66+ // when the job depends on other jobs, we cannot evaluate its concurrency, so it should be blocked and will be evaluated again when its dependencies are done
67+ return true , nil
68+ }
69+
70+ if job .ConcurrencyGroup == "" || job .ConcurrencyCancel {
71+ return false , nil
72+ }
73+
74+ runs , jobs , err := actions_model .GetConcurrentRunsAndJobs (ctx , job .RepoID , job .ConcurrencyGroup , []actions_model.Status {actions_model .StatusRunning })
75+ if err != nil {
76+ return false , fmt .Errorf ("GetConcurrentRunsAndJobs: %w" , err )
77+ }
78+
79+ return len (runs ) > 0 || len (jobs ) > 0 , nil
80+ }
81+
82+ // PrepareToStartJobWithConcurrency prepares a job to start by its evaluated concurrency group and cancelling previous jobs if necessary.
6583// It returns the new status of the job (either StatusBlocked or StatusWaiting) and any error encountered during the process.
6684func PrepareToStartJobWithConcurrency (ctx context.Context , job * actions_model.ActionRunJob ) (actions_model.Status , error ) {
67- if actions_model .ShouldWaitJobForConcurrencyEvaluation (job ) {
68- return actions_model .StatusBlocked , nil
69- }
70- shouldBlock , err := actions_model .ShouldBlockJobByConcurrency (ctx , job )
85+ shouldBlock , err := shouldBlockJobByConcurrency (ctx , job )
7186 if err != nil {
7287 return actions_model .StatusBlocked , err
7388 }
@@ -79,10 +94,23 @@ func PrepareToStartJobWithConcurrency(ctx context.Context, job *actions_model.Ac
7994 return actions_model .StatusWaiting , err
8095}
8196
97+ func shouldBlockRunByConcurrency (ctx context.Context , actionRun * actions_model.ActionRun ) (bool , error ) {
98+ if actionRun .ConcurrencyGroup == "" || actionRun .ConcurrencyCancel {
99+ return false , nil
100+ }
101+
102+ runs , jobs , err := actions_model .GetConcurrentRunsAndJobs (ctx , actionRun .RepoID , actionRun .ConcurrencyGroup , []actions_model.Status {actions_model .StatusRunning })
103+ if err != nil {
104+ return false , fmt .Errorf ("find concurrent runs and jobs: %w" , err )
105+ }
106+
107+ return len (runs ) > 0 || len (jobs ) > 0 , nil
108+ }
109+
82110// PrepareToStartRunWithConcurrency prepares a run to start by its evaluated concurrency group and cancelling previous jobs if necessary.
83111// It returns the new status of the job (either StatusBlocked or StatusWaiting) and any error encountered during the process.
84112func PrepareToStartRunWithConcurrency (ctx context.Context , run * actions_model.ActionRun ) (actions_model.Status , error ) {
85- shouldBlock , err := actions_model . ShouldBlockRunByConcurrency (ctx , run )
113+ shouldBlock , err := shouldBlockRunByConcurrency (ctx , run )
86114 if err != nil {
87115 return actions_model .StatusBlocked , err
88116 }
0 commit comments