feat(workflow): record parallel branch steps as their own steps
This commit is contained in:
1 parent
5682a8c570
commit
320b9dc340
5 files changed
+259
-75
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/agentrun"
|
||||
"github.com/samcharles93/archie-core/internal/domain/stableid"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
const maxRetryAttempts = 10
|
||||
@@ -272,9 +273,28 @@ func compileParallel(step StepRecord, registry StepRegistry) (Stage, error) {
|
||||
|
||||
func runBranch(ctx context.Context, tc *TaskContext, stages []Stage) error {
|
||||
for _, stage := range stages {
|
||||
if err := stage.Run(ctx, tc); err != nil && !stage.ContinueOnFailure {
|
||||
parentID := tc.StepID
|
||||
stepID, _, err := tc.startChildStep(ctx, task.StepKindStage, branchStepName(tc.workflowBranch, stage.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tc.StepID = stepID
|
||||
runErr := stage.Run(ctx, tc)
|
||||
tc.StepID = parentID
|
||||
to, detail := taskstate.StepSucceeded, ""
|
||||
switch {
|
||||
case runErr == nil:
|
||||
case ctx.Err() != nil:
|
||||
to, detail = taskstate.StepInterrupted, runErr.Error()
|
||||
default:
|
||||
to, detail = taskstate.StepFailed, runErr.Error()
|
||||
}
|
||||
if err := tc.finishChildStep(ctx, stepID, to, detail, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if runErr != nil && !stage.ContinueOnFailure {
|
||||
return runErr
|
||||
}
|
||||
if tc.Outcome.Status != "" {
|
||||
return nil
|
||||
}
|
||||
@@ -282,6 +302,17 @@ func runBranch(ctx context.Context, tc *TaskContext, stages []Stage) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// branchStepName qualifies a branch stage's recorded name with its branch, so
|
||||
// two branches running the same step are distinct rows. It mirrors the name
|
||||
// RunAgentChild gives a branch's agent steps; a branch step and the agent it
|
||||
// runs may share a name, which is safe because they are different step kinds.
|
||||
func branchStepName(branch, name string) string {
|
||||
if branch == "" {
|
||||
return name
|
||||
}
|
||||
return branch + "/" + name
|
||||
}
|
||||
|
||||
// branch is a copy of the run's context for one parallel branch. The task is
|
||||
// copied too, with its usage counters zeroed, so join can add what the branch
|
||||
// spent without the branches racing on one task.
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// fakeStepStore records the step writes a branch makes.
|
||||
type fakeStepStore struct {
|
||||
started []task.StepStart
|
||||
finished []task.StepFinish
|
||||
nextID int64
|
||||
}
|
||||
|
||||
func (f *fakeStepStore) Update(context.Context, *task.Task) error { return nil }
|
||||
|
||||
func (f *fakeStepStore) Transition(context.Context, int64, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStepStore) InsertEvent(context.Context, events.Event) (int64, error) { return 0, nil }
|
||||
|
||||
func (f *fakeStepStore) StartStep(_ context.Context, s task.StepStart) (int64, events.Event, error) {
|
||||
f.nextID++
|
||||
f.started = append(f.started, s)
|
||||
return f.nextID, events.Event{ID: f.nextID}, nil
|
||||
}
|
||||
|
||||
func (f *fakeStepStore) FinishStep(_ context.Context, s task.StepFinish) (events.Event, error) {
|
||||
f.finished = append(f.finished, s)
|
||||
return events.Event{ID: s.StepID}, nil
|
||||
}
|
||||
|
||||
// TestRunBranchRecordsChildSteps pins the step contract a branch owes the
|
||||
// canvas: every branch step is a stage row parented to the parallel step and
|
||||
// named for its branch, and its outcome is recorded.
|
||||
func TestRunBranchRecordsChildSteps(t *testing.T) {
|
||||
boom := errors.New("boom")
|
||||
tests := []struct {
|
||||
name string
|
||||
stage Stage
|
||||
wantErr error
|
||||
wantStatus taskstate.StepStatus
|
||||
wantDetail string
|
||||
}{
|
||||
{
|
||||
name: "a passing branch step",
|
||||
stage: Stage{Name: "agent.run", Run: func(context.Context, *TaskContext) error { return nil }},
|
||||
wantStatus: taskstate.StepSucceeded,
|
||||
},
|
||||
{
|
||||
name: "a failing branch step",
|
||||
stage: Stage{Name: "command.run", Run: func(context.Context, *TaskContext) error { return boom }},
|
||||
wantErr: boom,
|
||||
wantStatus: taskstate.StepFailed,
|
||||
wantDetail: "boom",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := &fakeStepStore{}
|
||||
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1}, Store: store, StepID: 7, workflowBranch: "docs"}
|
||||
err := runBranch(context.Background(), tc, []Stage{tt.stage})
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
||||
}
|
||||
if len(store.started) != 1 {
|
||||
t.Fatalf("started %d steps, want 1", len(store.started))
|
||||
}
|
||||
start := store.started[0]
|
||||
if start.Kind != task.StepKindStage || start.ParentID != 7 || start.Name != "docs/"+tt.stage.Name {
|
||||
t.Fatalf("start = %#v, want a stage named %q under step 7", start, "docs/"+tt.stage.Name)
|
||||
}
|
||||
if len(store.finished) != 1 {
|
||||
t.Fatalf("finished %d steps, want 1", len(store.finished))
|
||||
}
|
||||
finish := store.finished[0]
|
||||
if finish.To != tt.wantStatus || finish.Detail != tt.wantDetail {
|
||||
t.Fatalf("finish = %#v, want %s %q", finish, tt.wantStatus, tt.wantDetail)
|
||||
}
|
||||
if tc.StepID != 7 {
|
||||
t.Fatalf("branch step leaked its step id: %d", tc.StepID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user