Skip to content

Commit da33b70

Browse files
lunnyZettat123wxiaoguang
authored
Add a transaction to pickTask (#33543) (#33563)
Backport #33543 In the old `pickTask`, when getting secrets or variables failed, the task could get stuck in the `running` status (task status is `running` but the runner did not fetch the task). To fix this issue, these steps should be in one transaction. --------- Co-authored-by: Zettat123 <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 8fa3925 commit da33b70

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

routers/api/actions/runner/runner.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (s *Service) FetchTask(
156156
// if the task version in request is not equal to the version in db,
157157
// it means there may still be some tasks not be assgined.
158158
// try to pick a task for the runner that send the request.
159-
if t, ok, err := pickTask(ctx, runner); err != nil {
159+
if t, ok, err := actions_service.PickTask(ctx, runner); err != nil {
160160
log.Error("pick task failed: %v", err)
161161
return nil, status.Errorf(codes.Internal, "pick task: %v", err)
162162
} else if ok {

services/actions/init_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import (
1717
)
1818

1919
func TestMain(m *testing.M) {
20-
unittest.MainTest(m, &unittest.TestOptions{
21-
FixtureFiles: []string{"action_runner_token.yml"},
22-
})
20+
unittest.MainTest(m)
2321
os.Exit(m.Run())
2422
}
2523

routers/api/actions/runner/utils.go renamed to services/actions/task.go

+55-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2022 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package runner
4+
package actions
55

66
import (
77
"context"
@@ -16,51 +16,68 @@ import (
1616
"code.gitea.io/gitea/modules/json"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19-
"code.gitea.io/gitea/services/actions"
2019

2120
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
2221
"google.golang.org/protobuf/types/known/structpb"
2322
)
2423

25-
func pickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv1.Task, bool, error) {
26-
t, ok, err := actions_model.CreateTaskForRunner(ctx, runner)
27-
if err != nil {
28-
return nil, false, fmt.Errorf("CreateTaskForRunner: %w", err)
29-
}
30-
if !ok {
31-
return nil, false, nil
32-
}
24+
func PickTask(ctx context.Context, runner *actions_model.ActionRunner) (*runnerv1.Task, bool, error) {
25+
var (
26+
task *runnerv1.Task
27+
job *actions_model.ActionRunJob
28+
)
3329

34-
secrets, err := secret_model.GetSecretsOfTask(ctx, t)
35-
if err != nil {
36-
return nil, false, fmt.Errorf("GetSecretsOfTask: %w", err)
37-
}
30+
if err := db.WithTx(ctx, func(ctx context.Context) error {
31+
t, ok, err := actions_model.CreateTaskForRunner(ctx, runner)
32+
if err != nil {
33+
return fmt.Errorf("CreateTaskForRunner: %w", err)
34+
}
35+
if !ok {
36+
return nil
37+
}
3838

39-
vars, err := actions_model.GetVariablesOfRun(ctx, t.Job.Run)
40-
if err != nil {
41-
return nil, false, fmt.Errorf("GetVariablesOfRun: %w", err)
42-
}
39+
if err := t.LoadAttributes(ctx); err != nil {
40+
return fmt.Errorf("task LoadAttributes: %w", err)
41+
}
42+
job = t.Job
4343

44-
actions.CreateCommitStatus(ctx, t.Job)
44+
secrets, err := secret_model.GetSecretsOfTask(ctx, t)
45+
if err != nil {
46+
return fmt.Errorf("GetSecretsOfTask: %w", err)
47+
}
48+
49+
vars, err := actions_model.GetVariablesOfRun(ctx, t.Job.Run)
50+
if err != nil {
51+
return fmt.Errorf("GetVariablesOfRun: %w", err)
52+
}
53+
54+
needs, err := findTaskNeeds(ctx, job)
55+
if err != nil {
56+
return fmt.Errorf("findTaskNeeds: %w", err)
57+
}
58+
59+
taskContext := generateTaskContext(t)
4560

46-
task := &runnerv1.Task{
47-
Id: t.ID,
48-
WorkflowPayload: t.Job.WorkflowPayload,
49-
Context: generateTaskContext(t),
50-
Secrets: secrets,
51-
Vars: vars,
61+
task = &runnerv1.Task{
62+
Id: t.ID,
63+
WorkflowPayload: t.Job.WorkflowPayload,
64+
Context: taskContext,
65+
Secrets: secrets,
66+
Vars: vars,
67+
Needs: needs,
68+
}
69+
70+
return nil
71+
}); err != nil {
72+
return nil, false, err
5273
}
5374

54-
if needs, err := findTaskNeeds(ctx, t); err != nil {
55-
log.Error("Cannot find needs for task %v: %v", t.ID, err)
56-
// Go on with empty needs.
57-
// If return error, the task will be wild, which means the runner will never get it when it has been assigned to the runner.
58-
// In contrast, missing needs is less serious.
59-
// And the task will fail and the runner will report the error in the logs.
60-
} else {
61-
task.Needs = needs
75+
if task == nil {
76+
return nil, false, nil
6277
}
6378

79+
CreateCommitStatus(ctx, job)
80+
6481
return task, true, nil
6582
}
6683

@@ -95,7 +112,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
95112

96113
refName := git.RefName(ref)
97114

98-
giteaRuntimeToken, err := actions.CreateAuthorizationToken(t.ID, t.Job.RunID, t.JobID)
115+
giteaRuntimeToken, err := CreateAuthorizationToken(t.ID, t.Job.RunID, t.JobID)
99116
if err != nil {
100117
log.Error("actions.CreateAuthorizationToken failed: %v", err)
101118
}
@@ -148,16 +165,13 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
148165
return taskContext
149166
}
150167

151-
func findTaskNeeds(ctx context.Context, task *actions_model.ActionTask) (map[string]*runnerv1.TaskNeed, error) {
152-
if err := task.LoadAttributes(ctx); err != nil {
153-
return nil, fmt.Errorf("LoadAttributes: %w", err)
154-
}
155-
if len(task.Job.Needs) == 0 {
168+
func findTaskNeeds(ctx context.Context, job *actions_model.ActionRunJob) (map[string]*runnerv1.TaskNeed, error) {
169+
if len(job.Needs) == 0 {
156170
return nil, nil
157171
}
158-
needs := container.SetOf(task.Job.Needs...)
172+
needs := container.SetOf(job.Needs...)
159173

160-
jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: task.Job.RunID})
174+
jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: job.RunID})
161175
if err != nil {
162176
return nil, fmt.Errorf("FindRunJobs: %w", err)
163177
}

routers/api/actions/runner/utils_test.go renamed to services/actions/task_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2024 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package runner
4+
package actions
55

66
import (
77
"context"
@@ -17,8 +17,9 @@ func Test_findTaskNeeds(t *testing.T) {
1717
assert.NoError(t, unittest.PrepareTestDatabase())
1818

1919
task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 51})
20+
job := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunJob{ID: task.JobID})
2021

21-
ret, err := findTaskNeeds(context.Background(), task)
22+
ret, err := findTaskNeeds(context.Background(), job)
2223
assert.NoError(t, err)
2324
assert.Len(t, ret, 1)
2425
assert.Contains(t, ret, "job1")

0 commit comments

Comments
 (0)