feat(workflow): record steps skipped by when as skipped
This commit is contained in:
1 parent
320b9dc340
commit
0598a357dd
15 files changed
+93
-25
No files matched your search
@@ -207,8 +207,10 @@ func wrapStep(step StepRecord, body func(context.Context, *TaskContext) error) S
|
||||
}
|
||||
return Stage{Name: name, ContinueOnFailure: step.OnFailure == onFailureContinue, Run: func(ctx context.Context, tc *TaskContext) error {
|
||||
tc.stepResult = StepResult{}
|
||||
tc.skippedWhen = ""
|
||||
if step.When != "" && !when.holds(tc) {
|
||||
tc.Log.Info("step skipped", "step", name, "when", step.When)
|
||||
tc.skippedWhen = step.When
|
||||
return nil
|
||||
}
|
||||
err := runAttempts(ctx, tc, name, attempts, backoff, body)
|
||||
@@ -281,13 +283,9 @@ func runBranch(ctx context.Context, tc *TaskContext, stages []Stage) error {
|
||||
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 := stageOutcome(tc.skippedWhen, runErr)
|
||||
if runErr != nil && 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
|
||||
|
||||
@@ -3,6 +3,7 @@ package workflow
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
@@ -60,11 +61,17 @@ func TestRunBranchRecordsChildSteps(t *testing.T) {
|
||||
wantStatus: taskstate.StepFailed,
|
||||
wantDetail: "boom",
|
||||
},
|
||||
{
|
||||
name: "a branch step whose when was false",
|
||||
stage: wrapStep(StepRecord{ID: "agent.run", When: "task.title"}, func(context.Context, *TaskContext) error { return nil }),
|
||||
wantStatus: taskstate.StepSkipped,
|
||||
wantDetail: "task.title",
|
||||
},
|
||||
}
|
||||
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"}
|
||||
tc := &TaskContext{Task: &task.Task{ID: 1, Attempt: 1}, Store: store, StepID: 7, workflowBranch: "docs", Log: slog.Default()}
|
||||
err := runBranch(context.Background(), tc, []Stage{tt.stage})
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("err = %v, want %v", err, tt.wantErr)
|
||||
|
||||
@@ -81,6 +81,11 @@ type TaskContext struct {
|
||||
// stepResults holds each finished step's, by step id.
|
||||
stepResult StepResult
|
||||
stepResults map[string]StepResult
|
||||
// skippedWhen is the when condition that skipped the stage whose body just
|
||||
// ran; empty when the stage ran. wrapStep sets it and the engine reads it
|
||||
// to record the step as skipped. It is per-attempt scratch state, reset by
|
||||
// wrapStep before every step.
|
||||
skippedWhen string
|
||||
|
||||
Task *Task
|
||||
Repo config.Repo
|
||||
@@ -355,12 +360,7 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
|
||||
return
|
||||
}
|
||||
|
||||
finishTo := taskstate.StepSucceeded
|
||||
finishDetail := ""
|
||||
if err != nil {
|
||||
finishTo = taskstate.StepFailed
|
||||
finishDetail = err.Error()
|
||||
}
|
||||
finishTo, finishDetail := stageOutcome(tc.skippedWhen, err)
|
||||
// The outcome event is the store's own stage_finish row, written in the
|
||||
// FinishStep transaction -- not a second copy emitted beside it. A
|
||||
// failed recording write parks the execution, like a failed start.
|
||||
@@ -384,6 +384,21 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
|
||||
finish(ctx, tc, log)
|
||||
}
|
||||
|
||||
// stageOutcome is how a stage that ran to its end records: a stage whose when
|
||||
// was false is skipped, with the condition as its detail; an error fails it;
|
||||
// anything else succeeds. Interruption is the caller's call, because it owns
|
||||
// the detached context the record needs.
|
||||
func stageOutcome(skippedWhen string, err error) (taskstate.StepStatus, string) {
|
||||
switch {
|
||||
case skippedWhen != "":
|
||||
return taskstate.StepSkipped, skippedWhen
|
||||
case err != nil:
|
||||
return taskstate.StepFailed, err.Error()
|
||||
default:
|
||||
return taskstate.StepSucceeded, ""
|
||||
}
|
||||
}
|
||||
|
||||
// resultsAfter is what a stage records for a later attempt resumed after it:
|
||||
// the results so far when the run moves past the stage, else nothing.
|
||||
func (tc *TaskContext) resultsAfter(stage Stage, err error) []byte {
|
||||
|
||||
@@ -233,6 +233,10 @@ func stepEventData(to taskstate.StepStatus, durationMS int64, detail string) map
|
||||
data["error"] = detail
|
||||
case taskstate.StepInterrupted:
|
||||
data["interrupted"] = true
|
||||
case taskstate.StepSkipped:
|
||||
// The detail is the when condition that skipped the step.
|
||||
data["skipped"] = true
|
||||
data["when"] = detail
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -16,6 +16,9 @@ const (
|
||||
StepFailed StepStatus = "failed"
|
||||
StepCancelled StepStatus = "cancelled"
|
||||
StepInterrupted StepStatus = "interrupted"
|
||||
// StepSkipped is a step whose when condition was false: it never ran, but
|
||||
// it is still a step of the attempt and is recorded as one.
|
||||
StepSkipped StepStatus = "skipped"
|
||||
)
|
||||
|
||||
// executionTransitions lists every legal execution status move. Terminal
|
||||
@@ -38,12 +41,13 @@ var executionTransitions = map[string][]string{
|
||||
// vocabulary.
|
||||
var stepTransitions = map[StepStatus][]StepStatus{
|
||||
StepPending: {StepRunning, StepCancelled},
|
||||
StepRunning: {StepSucceeded, StepFailed, StepCancelled, StepInterrupted},
|
||||
StepRunning: {StepSucceeded, StepFailed, StepCancelled, StepInterrupted, StepSkipped},
|
||||
|
||||
StepSucceeded: nil,
|
||||
StepFailed: nil,
|
||||
StepCancelled: nil,
|
||||
StepInterrupted: nil,
|
||||
StepSkipped: nil,
|
||||
}
|
||||
|
||||
// CanTransition reports whether a WorkflowExecution may move from one status to
|
||||
@@ -63,7 +67,7 @@ func CanStepTransition(from, to StepStatus) bool {
|
||||
// only moves again as a fresh step in a new attempt.
|
||||
func StepTerminal(status StepStatus) bool {
|
||||
switch status {
|
||||
case StepSucceeded, StepFailed, StepCancelled, StepInterrupted:
|
||||
case StepSucceeded, StepFailed, StepCancelled, StepInterrupted, StepSkipped:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package taskstate
|
||||
|
||||
import "testing"
|
||||
|
||||
// The step guard is the store's own enforcement of which step moves are legal.
|
||||
// A skipped step is a running step's outcome and a terminal state: one that
|
||||
// never started cannot skip, and one that skipped never moves again.
|
||||
func TestStepTransitions(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
from, to StepStatus
|
||||
want bool
|
||||
}{
|
||||
{"a running step is skipped", StepRunning, StepSkipped, true},
|
||||
{"a running step still succeeds", StepRunning, StepSucceeded, true},
|
||||
{"a pending step is not skipped", StepPending, StepSkipped, false},
|
||||
{"a skipped step does not move", StepSkipped, StepRunning, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := CanStepTransition(tt.from, tt.to); got != tt.want {
|
||||
t.Fatalf("CanStepTransition(%s, %s) = %v, want %v", tt.from, tt.to, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ const (
|
||||
attemptStatusFailed = "failed"
|
||||
attemptStatusInterrupted = "interrupted"
|
||||
attemptStatusRunning = "running"
|
||||
attemptStatusSkipped = "skipped"
|
||||
attemptStatusUnknown = "unknown"
|
||||
)
|
||||
|
||||
@@ -284,6 +285,8 @@ func mapStepStatus(status taskstate.StepStatus, inFlight bool) string {
|
||||
return attemptStatusFailed
|
||||
case taskstate.StepCancelled, taskstate.StepInterrupted:
|
||||
return attemptStatusInterrupted
|
||||
case taskstate.StepSkipped:
|
||||
return attemptStatusSkipped
|
||||
case taskstate.StepRunning:
|
||||
if inFlight {
|
||||
return attemptStatusRunning
|
||||
|
||||
Reference in new issue
Block a user