Improve updating Actions tasks (#24600)

Co-authored-by: Giteabot <teabot@gitea.io>
This commit is contained in:
Jason Song 2023-05-10 19:54:18 +08:00 committed by GitHub
parent ea7954f069
commit 2d0ff00823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 28 deletions

View file

@ -291,7 +291,7 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
} }
task.LogFilename = logFileName(job.Run.Repo.FullName(), task.ID) task.LogFilename = logFileName(job.Run.Repo.FullName(), task.ID)
if _, err := e.ID(task.ID).Cols("log_filename").Update(task); err != nil { if err := UpdateTask(ctx, task, "log_filename"); err != nil {
return nil, false, err return nil, false, err
} }
@ -367,9 +367,18 @@ func UpdateTaskByState(ctx context.Context, state *runnerv1.TaskState) (*ActionT
return nil, util.ErrNotExist return nil, util.ErrNotExist
} }
if task.Status.IsDone() {
// the state is final, do nothing
return task, nil
}
// state.Result is not unspecified means the task is finished
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED { if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
task.Status = Status(state.Result) task.Status = Status(state.Result)
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix()) task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
if err := UpdateTask(ctx, task, "status", "stopped"); err != nil {
return nil, err
}
if _, err := UpdateRunJob(ctx, &ActionRunJob{ if _, err := UpdateRunJob(ctx, &ActionRunJob{
ID: task.JobID, ID: task.JobID,
Status: task.Status, Status: task.Status,
@ -379,10 +388,6 @@ func UpdateTaskByState(ctx context.Context, state *runnerv1.TaskState) (*ActionT
} }
} }
if _, err := e.ID(task.ID).Update(task); err != nil {
return nil, err
}
if err := task.LoadAttributes(ctx); err != nil { if err := task.LoadAttributes(ctx); err != nil {
return nil, err return nil, err
} }
@ -440,7 +445,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
return err return err
} }
if _, err := e.ID(task.ID).Update(task); err != nil { if err := UpdateTask(ctx, task, "status", "stopped"); err != nil {
return err return err
} }

View file

@ -10,7 +10,6 @@ import (
actions_model "code.gitea.io/gitea/models/actions" actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/actions" "code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
actions_service "code.gitea.io/gitea/services/actions" actions_service "code.gitea.io/gitea/services/actions"
@ -120,27 +119,7 @@ func (s *Service) UpdateTask(
ctx context.Context, ctx context.Context,
req *connect.Request[runnerv1.UpdateTaskRequest], req *connect.Request[runnerv1.UpdateTaskRequest],
) (*connect.Response[runnerv1.UpdateTaskResponse], error) { ) (*connect.Response[runnerv1.UpdateTaskResponse], error) {
{ task, err := actions_model.UpdateTaskByState(ctx, req.Msg.State)
// to debug strange runner behaviors, it could be removed if all problems have been solved.
stateMsg, _ := json.Marshal(req.Msg.State)
log.Trace("update task with state: %s", stateMsg)
}
// Get Task first
task, err := actions_model.GetTaskByID(ctx, req.Msg.State.Id)
if err != nil {
return nil, status.Errorf(codes.Internal, "can't find the task: %v", err)
}
if task.Status.IsCancelled() {
return connect.NewResponse(&runnerv1.UpdateTaskResponse{
State: &runnerv1.TaskState{
Id: req.Msg.State.Id,
Result: task.Status.AsResult(),
},
}), nil
}
task, err = actions_model.UpdateTaskByState(ctx, req.Msg.State)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "update task: %v", err) return nil, status.Errorf(codes.Internal, "update task: %v", err)
} }