executeTaskRun executes a task run that is already in RUNNING status.
(ctx context.Context, projectID string, taskRunUID, taskUID int64)
| 69 | |
| 70 | // executeTaskRun executes a task run that is already in RUNNING status. |
| 71 | func (s *Scheduler) executeTaskRun(ctx context.Context, projectID string, taskRunUID, taskUID int64) error { |
| 72 | task, err := s.store.GetTaskByID(ctx, projectID, taskUID) |
| 73 | if err != nil { |
| 74 | return errors.Wrapf(err, "failed to get task") |
| 75 | } |
| 76 | if task == nil { |
| 77 | return errors.Errorf("task %v not found", taskUID) |
| 78 | } |
| 79 | |
| 80 | // Validate task freshness before execution. |
| 81 | if err := s.validateTaskFreshness(ctx, task); err != nil { |
| 82 | slog.WarnContext(ctx, "task run blocked by drift validation", log.BBError(err)) |
| 83 | taskRunStatusPatch := &store.TaskRunStatusPatch{ |
| 84 | ID: taskRunUID, |
| 85 | ProjectID: task.ProjectID, |
| 86 | Updater: "", |
| 87 | Status: storepb.TaskRun_FAILED, |
| 88 | AllowedStatuses: []storepb.TaskRun_Status{ |
| 89 | storepb.TaskRun_RUNNING, |
| 90 | }, |
| 91 | ResultProto: &storepb.TaskRunResult{ |
| 92 | Detail: err.Error(), |
| 93 | }, |
| 94 | } |
| 95 | if _, patchErr := s.store.UpdateTaskRunStatus(ctx, taskRunStatusPatch); patchErr != nil { |
| 96 | if common.ErrorCode(patchErr) == common.Conflict { |
| 97 | return nil |
| 98 | } |
| 99 | return errors.Wrapf(patchErr, "failed to mark task run as failed after drift detection") |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | executor, ok := s.executorMap[task.Type] |
| 105 | if !ok { |
| 106 | return errors.Errorf("executor not found for task type: %v", task.Type) |
| 107 | } |
| 108 | |
| 109 | // Update started_at |
| 110 | if err := s.store.UpdateTaskRunStartAt(ctx, task.ProjectID, taskRunUID); err != nil { |
| 111 | return errors.Wrapf(err, "failed to update task run start at") |
| 112 | } |
| 113 | |
| 114 | go s.runTaskRunOnce(ctx, taskRunUID, task, executor) |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int64, task *store.TaskMessage, executor Executor) { |
| 119 | ctx = taskRunLogContext(ctx, task.ProjectID, taskRunUID) |
no test coverage detected