Skip to content

Commit 06a7a8c

Browse files
committed
simplify code
1 parent aea9090 commit 06a7a8c

File tree

5 files changed

+55
-43
lines changed

5 files changed

+55
-43
lines changed

models/actions/run.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,35 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
374374
}
375375

376376
type ActionRunIndex db.ResourceIndex
377+
378+
func ShouldBlockRunByConcurreny(ctx context.Context, actionRun *ActionRun) (bool, error) {
379+
if len(actionRun.ConcurrencyGroup) == 0 {
380+
return false, nil
381+
}
382+
if actionRun.ConcurrencyCancel {
383+
return false, CancelConcurrentRuns(ctx, actionRun)
384+
}
385+
386+
concurrentRunsNum, err := db.Count[ActionRun](ctx, &FindRunOptions{
387+
RepoID: actionRun.RepoID,
388+
ConcurrencyGroup: actionRun.ConcurrencyGroup,
389+
Status: []Status{StatusWaiting, StatusRunning},
390+
})
391+
if err != nil {
392+
return false, fmt.Errorf("count running and waiting runs: %w", err)
393+
}
394+
395+
return concurrentRunsNum > 0, nil
396+
}
397+
398+
func CancelConcurrentRuns(ctx context.Context, actionRun *ActionRun) error {
399+
return CancelPreviousJobsWithOpts(ctx, &FindRunOptions{
400+
RepoID: actionRun.RepoID,
401+
ConcurrencyGroup: actionRun.ConcurrencyGroup,
402+
Status: []Status{
403+
StatusRunning,
404+
StatusWaiting,
405+
StatusBlocked,
406+
},
407+
})
408+
}

models/actions/run_job.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func AggregateJobStatus(jobs []*ActionRunJob) Status {
192192
}
193193
}
194194

195-
func ShouldJobBeBlockedByConcurrentJobs(ctx context.Context, actionRunJob *ActionRunJob) (bool, error) {
195+
func ShouldBlockJobByConcurreny(ctx context.Context, actionRunJob *ActionRunJob) (bool, error) {
196196
if len(actionRunJob.RawConcurrencyGroup) == 0 {
197197
return false, nil
198198
}
@@ -212,7 +212,7 @@ func ShouldJobBeBlockedByConcurrentJobs(ctx context.Context, actionRunJob *Actio
212212
Statuses: []Status{StatusRunning, StatusWaiting},
213213
})
214214
if err != nil {
215-
return false, fmt.Errorf("count waiting jobs: %w", err)
215+
return false, fmt.Errorf("count running and waiting jobs: %w", err)
216216
}
217217

218218
return concurrentJobsNum > 0, nil

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func Approve(ctx *context_module.Context) {
560560
return err
561561
}
562562
for _, job := range jobs {
563-
blockedByConcurrency, err := actions_model.ShouldJobBeBlockedByConcurrentJobs(ctx, job)
563+
blockedByConcurrency, err := actions_model.ShouldBlockJobByConcurreny(ctx, job)
564564
if err != nil {
565565
return err
566566
}

services/actions/job_emitter.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ func checkJobsByRunID(ctx context.Context, runID int64) error {
6969
if err != nil {
7070
return err
7171
}
72-
for _, cRun := range concurrentRuns {
73-
concurrentRunIDs.Add(cRun.ID)
74-
if cRun.NeedApproval {
72+
for _, concurrentRun := range concurrentRuns {
73+
concurrentRunIDs.Add(concurrentRun.ID)
74+
if concurrentRun.NeedApproval {
7575
continue
7676
}
77-
if err := checkJobsOfRun(ctx, cRun); err != nil {
77+
if err := checkJobsOfRun(ctx, concurrentRun); err != nil {
7878
return err
7979
}
80-
updatedRun, err := actions_model.GetRunByID(ctx, cRun.ID)
80+
updatedRun, err := actions_model.GetRunByID(ctx, concurrentRun.ID)
8181
if err != nil {
8282
return err
8383
}
@@ -103,21 +103,21 @@ func checkJobsByRunID(ctx context.Context, runID int64) error {
103103
if err != nil {
104104
return err
105105
}
106-
for _, cJob := range concurrentJobs {
107-
if concurrentRunIDs.Contains(cJob.RunID) {
106+
for _, concurrentJob := range concurrentJobs {
107+
if concurrentRunIDs.Contains(concurrentJob.RunID) {
108108
continue
109109
}
110-
cRun, err := actions_model.GetRunByID(ctx, cJob.RunID)
110+
concurrentRun, err := actions_model.GetRunByID(ctx, concurrentJob.RunID)
111111
if err != nil {
112112
return err
113113
}
114-
if cRun.NeedApproval {
114+
if concurrentRun.NeedApproval {
115115
continue
116116
}
117-
if err := checkJobsOfRun(ctx, cRun); err != nil {
117+
if err := checkJobsOfRun(ctx, concurrentRun); err != nil {
118118
return err
119119
}
120-
updatedJob, err := actions_model.GetRunJobByID(ctx, cJob.ID)
120+
updatedJob, err := actions_model.GetRunJobByID(ctx, concurrentJob.ID)
121121
if err != nil {
122122
return err
123123
}
@@ -309,5 +309,5 @@ func checkConcurrencyForJobWithNeeds(ctx context.Context, actionRunJob *actions_
309309
}
310310
}
311311

312-
return actions_model.ShouldJobBeBlockedByConcurrentJobs(ctx, actionRunJob)
312+
return actions_model.ShouldBlockJobByConcurreny(ctx, actionRunJob)
313313
}

services/actions/run.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,13 @@ func InsertRun(ctx context.Context, run *actions_model.ActionRun, jobs []*jobpar
3131
run.Index = index
3232
run.Title = util.EllipsisDisplayString(run.Title, 255)
3333

34-
// check workflow concurrency
35-
if len(run.ConcurrencyGroup) > 0 {
36-
if run.ConcurrencyCancel {
37-
if err := actions_model.CancelPreviousJobsWithOpts(ctx, &actions_model.FindRunOptions{
38-
RepoID: run.RepoID,
39-
ConcurrencyGroup: run.ConcurrencyGroup,
40-
Status: []actions_model.Status{
41-
actions_model.StatusRunning,
42-
actions_model.StatusWaiting,
43-
actions_model.StatusBlocked,
44-
},
45-
}); err != nil {
46-
return err
47-
}
48-
} else {
49-
concurrentRunsNum, err := db.Count[actions_model.ActionRun](ctx, &actions_model.FindRunOptions{
50-
RepoID: run.RepoID,
51-
ConcurrencyGroup: run.ConcurrencyGroup,
52-
Status: []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusRunning},
53-
})
54-
if err != nil {
55-
return err
56-
}
57-
if concurrentRunsNum > 0 {
58-
run.Status = actions_model.StatusBlocked
59-
}
60-
}
34+
// check run (workflow-level) concurrency
35+
shouldBlockRunByConcurreny, err := actions_model.ShouldBlockRunByConcurreny(ctx, run)
36+
if err != nil {
37+
return err
38+
}
39+
if shouldBlockRunByConcurreny {
40+
run.Status = actions_model.StatusBlocked
6141
}
6242

6343
if err := db.Insert(ctx, run); err != nil {
@@ -125,7 +105,7 @@ func InsertRun(ctx context.Context, run *actions_model.ActionRun, jobs []*jobpar
125105
}
126106
runJob.IsConcurrencyEvaluated = true
127107
// check if the job should be blocked by job concurrency
128-
shouldBlock, err := actions_model.ShouldJobBeBlockedByConcurrentJobs(ctx, runJob)
108+
shouldBlock, err := actions_model.ShouldBlockJobByConcurreny(ctx, runJob)
129109
if err != nil {
130110
return err
131111
}

0 commit comments

Comments
 (0)