Skip to content

Commit bee8d1c

Browse files
committed
feat: Update the db migration and the add a debug message about wrong parsed values
1 parent 6d8292c commit bee8d1c

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

models/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func prepareMigrationTasks() []*migration {
400400
newMigration(323, "Add support for actions concurrency", v1_26.AddActionsConcurrency),
401401
newMigration(324, "Fix closed milestone completeness for milestones with no issues", v1_26.FixClosedMilestoneCompleteness),
402402
newMigration(325, "Fix missed repo_id when migrate attachments", v1_26.FixMissedRepoIDWhenMigrateAttachments),
403-
newMigration(326, "Add runner capacity and job max-parallel support", v1_26.AddRunnerCapacityAndJobMaxParallel),
403+
newMigration(326, "Add job max-parallel support", v1_26.AddJobMaxParallel),
404404
}
405405
return preparedMigrations
406406
}

models/migrations/v1_26/v326.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package v1_26
2+
3+
import "xorm.io/xorm"
4+
5+
func AddJobMaxParallel(x *xorm.Engine) error {
6+
type ActionRunJob struct {
7+
MaxParallel int
8+
}
9+
10+
return x.Sync(new(ActionRunJob))
11+
}

services/actions/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
actions_model "code.gitea.io/gitea/models/actions"
1212
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/util"
1415
notify_service "code.gitea.io/gitea/services/notify"
1516

@@ -133,6 +134,8 @@ func InsertRun(ctx context.Context, run *actions_model.ActionRun, jobs []*jobpar
133134
if job.Strategy.MaxParallelString != "" {
134135
if maxParallel, err := strconv.Atoi(job.Strategy.MaxParallelString); err == nil && maxParallel > 0 {
135136
runJob.MaxParallel = maxParallel
137+
} else {
138+
log.Debug("failed to process max-parallel for job %s: invalid value %v: %v", id, job.Strategy.MaxParallelString, err)
136139
}
137140
}
138141

services/actions/task_assignment_test.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
)
1616

17-
func TestCreateTaskForRunner_MaxParallelEnforcement(t *testing.T) {
17+
func TestMaxParallelJobStatusAndCounting(t *testing.T) {
1818
assert.NoError(t, unittest.PrepareTestDatabase())
1919

2020
t.Run("MaxParallelReached", func(t *testing.T) {
@@ -117,4 +117,72 @@ func TestCreateTaskForRunner_MaxParallelEnforcement(t *testing.T) {
117117
assert.NoError(t, err)
118118
assert.Equal(t, 5, runningCount, "All jobs should be able to run without limit")
119119
})
120+
121+
t.Run("MaxParallelWrongValue", func(t *testing.T) {
122+
runID := int64(30000)
123+
jobID := "wrong-value-use-default-value-job"
124+
125+
// Create ActionRun first
126+
run := &actions_model.ActionRun{
127+
ID: runID,
128+
RepoID: 1,
129+
OwnerID: 1,
130+
Index: 30000,
131+
Status: actions_model.StatusRunning,
132+
}
133+
assert.NoError(t, db.Insert(context.Background(), run))
134+
135+
// Test different invalid max-parallel values
136+
testCases := []struct {
137+
name string
138+
maxParallel int
139+
description string
140+
}{
141+
{
142+
name: "negative value",
143+
maxParallel: -1,
144+
description: "Negative max-parallel should default to 0 (no limit)",
145+
},
146+
}
147+
148+
for _, tc := range testCases {
149+
t.Run(tc.name, func(t *testing.T) {
150+
151+
// Create jobs with the test max-parallel value
152+
for i := range 5 {
153+
job := &actions_model.ActionRunJob{
154+
RunID: runID,
155+
RepoID: 1,
156+
OwnerID: 1,
157+
JobID: jobID,
158+
Name: "Test Job " + tc.name,
159+
Status: actions_model.StatusWaiting,
160+
MaxParallel: tc.maxParallel,
161+
}
162+
assert.NoError(t, db.Insert(context.Background(), job))
163+
164+
// Verify the value was stored
165+
if i == 0 {
166+
storedJob, err := actions_model.GetRunJobByID(context.Background(), job.ID)
167+
assert.NoError(t, err)
168+
assert.Equal(t, tc.maxParallel, storedJob.MaxParallel, tc.description)
169+
}
170+
}
171+
172+
// All jobs can run simultaneously when max-parallel <= 0
173+
jobs, err := actions_model.GetRunJobsByRunID(context.Background(), runID)
174+
assert.NoError(t, err)
175+
176+
for _, job := range jobs {
177+
job.Status = actions_model.StatusRunning
178+
_, err := actions_model.UpdateRunJob(context.Background(), job, nil, "status")
179+
assert.NoError(t, err)
180+
}
181+
182+
runningCount, err := actions_model.CountRunningJobsByWorkflowAndRun(context.Background(), runID, jobID)
183+
assert.NoError(t, err)
184+
assert.GreaterOrEqual(t, runningCount, 5, "All jobs should be able to run when max-parallel is "+tc.name)
185+
})
186+
}
187+
})
120188
}

0 commit comments

Comments
 (0)