Skip to content

Commit d7dcd8b

Browse files
committed
fine tune
1 parent 90a31d0 commit d7dcd8b

File tree

5 files changed

+36
-45
lines changed

5 files changed

+36
-45
lines changed

models/actions/run.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -391,19 +391,6 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
391391

392392
type ActionRunIndex db.ResourceIndex
393393

394-
func ShouldBlockRunByConcurrency(ctx context.Context, actionRun *ActionRun) (bool, error) {
395-
if actionRun.ConcurrencyGroup == "" || actionRun.ConcurrencyCancel {
396-
return false, nil
397-
}
398-
399-
runs, jobs, err := GetConcurrentRunsAndJobs(ctx, actionRun.RepoID, actionRun.ConcurrencyGroup, []Status{StatusRunning})
400-
if err != nil {
401-
return false, fmt.Errorf("find concurrent runs and jobs: %w", err)
402-
}
403-
404-
return len(runs) > 0 || len(jobs) > 0, nil
405-
}
406-
407394
func GetConcurrentRunsAndJobs(ctx context.Context, repoID int64, concurrencyGroup string, status []Status) ([]*ActionRun, []*ActionRunJob, error) {
408395
runs, err := db.Find[ActionRun](ctx, &FindRunOptions{
409396
RepoID: repoID,

models/actions/run_job.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/models/db"
1313
repo_model "code.gitea.io/gitea/models/repo"
14+
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/timeutil"
1516
"code.gitea.io/gitea/modules/util"
1617

@@ -213,27 +214,6 @@ func AggregateJobStatus(jobs []*ActionRunJob) Status {
213214
}
214215
}
215216

216-
func ShouldWaitJobForConcurrencyEvaluation(job *ActionRunJob) bool {
217-
return job.RawConcurrency != "" && !job.IsConcurrencyEvaluated
218-
}
219-
220-
func ShouldBlockJobByConcurrency(ctx context.Context, job *ActionRunJob) (bool, error) {
221-
if ShouldWaitJobForConcurrencyEvaluation(job) {
222-
panic("job concurrency not evaluated, this function shouldn't be called")
223-
}
224-
225-
if job.ConcurrencyGroup == "" || job.ConcurrencyCancel {
226-
return false, nil
227-
}
228-
229-
runs, jobs, err := GetConcurrentRunsAndJobs(ctx, job.RepoID, job.ConcurrencyGroup, []Status{StatusRunning})
230-
if err != nil {
231-
return false, fmt.Errorf("GetConcurrentRunsAndJobs: %w", err)
232-
}
233-
234-
return len(runs) > 0 || len(jobs) > 0, nil
235-
}
236-
237217
func CancelPreviousJobsByJobConcurrency(ctx context.Context, job *ActionRunJob) (jobsToCancel []*ActionRunJob, _ error) {
238218
if job.RawConcurrency == "" {
239219
return nil, nil

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shou
522522
return fmt.Errorf("get run %d variables: %w", job.Run.ID, err)
523523
}
524524

525-
if job.RawConcurrency != "" && job.Status != actions_model.StatusBlocked {
525+
if job.RawConcurrency != "" && job.Status == actions_model.StatusWaiting {
526526
err = actions_service.EvaluateJobConcurrencyAndFillJobModel(ctx, job.Run, job, vars)
527527
if err != nil {
528528
return fmt.Errorf("evaluate job concurrency: %w", err)

services/actions/clear_tasks.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
6684
func 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.
84112
func 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
}

services/actions/job_emitter.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ func (r *jobStatusResolver) resolve(ctx context.Context) map[int64]actions_model
358358
}
359359

360360
func updateConcurrencyEvaluationForJobWithNeeds(ctx context.Context, actionRunJob *actions_model.ActionRunJob, vars map[string]string) error {
361-
if !actions_model.ShouldWaitJobForConcurrencyEvaluation(actionRunJob) {
362-
return nil
363-
}
364-
365361
if err := actionRunJob.LoadAttributes(ctx); err != nil {
366362
return err
367363
}

0 commit comments

Comments
 (0)