44package actions
55
66import (
7- "bytes"
87 "context"
98 "errors"
109 "fmt"
@@ -16,7 +15,6 @@ import (
1615 "code.gitea.io/gitea/modules/queue"
1716
1817 "github.com/nektos/act/pkg/jobparser"
19- act_model "github.com/nektos/act/pkg/model"
2018 "xorm.io/builder"
2119)
2220
@@ -230,22 +228,8 @@ func checkJobConcurrency(ctx context.Context, actionRunJob *actions_model.Action
230228 return false , nil
231229 }
232230
233- run := actionRunJob .Run
234-
235231 if len (actionRunJob .ConcurrencyGroup ) == 0 {
236- rawConcurrency := & act_model.RawConcurrency {
237- Group : actionRunJob .RawConcurrencyGroup ,
238- CancelInProgress : actionRunJob .RawConcurrencyCancel ,
239- }
240-
241- gitCtx := jobparser .ToGitContext (GenerateGitContext (run , actionRunJob ))
242-
243- actWorkflow , err := act_model .ReadWorkflow (bytes .NewReader (actionRunJob .WorkflowPayload ))
244- if err != nil {
245- return false , fmt .Errorf ("read workflow: %w" , err )
246- }
247- actJob := actWorkflow .GetJob (actionRunJob .JobID )
248-
232+ // empty concurrency group means the raw concurrency has not been evaluated
249233 task , err := actions_model .GetTaskByID (ctx , actionRunJob .TaskID )
250234 if err != nil {
251235 return false , fmt .Errorf ("get task by id: %w" , err )
@@ -254,7 +238,6 @@ func checkJobConcurrency(ctx context.Context, actionRunJob *actions_model.Action
254238 if err != nil {
255239 return false , fmt .Errorf ("find task needs: %w" , err )
256240 }
257-
258241 jobResults := make (map [string ]* jobparser.JobResult , len (taskNeeds ))
259242 for jobID , taskNeed := range taskNeeds {
260243 jobResult := & jobparser.JobResult {
@@ -264,7 +247,11 @@ func checkJobConcurrency(ctx context.Context, actionRunJob *actions_model.Action
264247 jobResults [jobID ] = jobResult
265248 }
266249
267- actionRunJob .ConcurrencyGroup , actionRunJob .ConcurrencyCancel = jobparser .InterpolatJobConcurrency (rawConcurrency , actJob , gitCtx , vars , jobResults )
250+ actionRunJob .ConcurrencyGroup , actionRunJob .ConcurrencyCancel , err = evaluateJobConcurrency (ctx , actionRunJob , vars , jobResults )
251+ if err != nil {
252+ return false , fmt .Errorf ("evaluate job concurrency: %w" , err )
253+ }
254+
268255 if _ , err := actions_model .UpdateRunJob (ctx , & actions_model.ActionRunJob {
269256 ID : actionRunJob .ID ,
270257 ConcurrencyGroup : actionRunJob .ConcurrencyGroup ,
@@ -274,35 +261,17 @@ func checkJobConcurrency(ctx context.Context, actionRunJob *actions_model.Action
274261 }
275262 }
276263
277- if actionRunJob .ConcurrencyCancel {
278- // cancel previous jobs in the same concurrency group
279- previousJobs , err := db .Find [actions_model.ActionRunJob ](ctx , actions_model.FindRunJobOptions {
280- RepoID : actionRunJob .RepoID ,
281- ConcurrencyGroup : actionRunJob .ConcurrencyGroup ,
282- Statuses : []actions_model.Status {
283- actions_model .StatusRunning ,
284- actions_model .StatusWaiting ,
285- actions_model .StatusBlocked ,
286- },
287- })
288- if err != nil {
289- return false , fmt .Errorf ("find previous jobs: %w" , err )
290- }
291- if err := actions_model .CancelJobs (ctx , previousJobs ); err != nil {
292- return false , fmt .Errorf ("cancel previous jobs: %w" , err )
293- }
294- // we have cancelled all previous jobs, so this job does not need to be blocked
264+ if len (actionRunJob .ConcurrencyGroup ) == 0 {
265+ // the job should not be blocked by concurrency if its concurrency group is empty
295266 return false , nil
296267 }
297268
298- waitingConcurrentJobsNum , err := db .Count [actions_model.ActionRunJob ](ctx , actions_model.FindRunJobOptions {
299- RepoID : actionRunJob .RepoID ,
300- ConcurrencyGroup : actionRunJob .ConcurrencyGroup ,
301- Statuses : []actions_model.Status {actions_model .StatusWaiting },
302- })
303- if err != nil {
304- return false , fmt .Errorf ("count waiting jobs: %w" , err )
269+ if actionRunJob .ConcurrencyCancel {
270+ if err := actions_model .CancelConcurrentJobs (ctx , actionRunJob ); err != nil {
271+ return false , fmt .Errorf ("cancel concurrent jobs: %w" , err )
272+ }
273+ return false , nil
305274 }
306275
307- return waitingConcurrentJobsNum > 0 , nil
276+ return actions_model . ShouldJobBeBlockedByConcurrentJobs ( ctx , actionRunJob )
308277}
0 commit comments