Cleanup state reporting (#3850)

This commit is contained in:
Anbraten
2024-07-01 11:20:55 +02:00
committed by GitHub
parent daeab8d3c7
commit 2fa9432ef8
18 changed files with 590 additions and 514 deletions

View File

@@ -24,9 +24,9 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)
func UpdateStepStatus(store store.Store, step *model.Step, state rpc.State) error {
func UpdateStepStatus(store store.Store, step *model.Step, state rpc.StepState) error {
if state.Exited {
step.Stopped = state.Finished
step.Finished = state.Finished
step.ExitCode = state.ExitCode
step.Error = state.Error
step.State = model.StatusSuccess
@@ -36,30 +36,30 @@ func UpdateStepStatus(store store.Store, step *model.Step, state rpc.State) erro
if state.ExitCode == pipeline.ExitCodeKilled {
step.State = model.StatusKilled
}
} else if step.Stopped == 0 {
} else if step.Finished == 0 {
step.Started = state.Started
step.State = model.StatusRunning
}
return store.StepUpdate(step)
}
func UpdateStepToStatusStarted(store store.Store, step model.Step, state rpc.State) (*model.Step, error) {
func UpdateStepToStatusStarted(store store.Store, step model.Step, state rpc.StepState) (*model.Step, error) {
step.Started = state.Started
step.State = model.StatusRunning
return &step, store.StepUpdate(&step)
}
func UpdateStepToStatusSkipped(store store.Store, step model.Step, stopped int64) (*model.Step, error) {
func UpdateStepToStatusSkipped(store store.Store, step model.Step, finished int64) (*model.Step, error) {
step.State = model.StatusSkipped
if step.Started != 0 {
step.State = model.StatusSuccess // for daemons that are killed
step.Stopped = stopped
step.Finished = finished
}
return &step, store.StepUpdate(&step)
}
func UpdateStepStatusToDone(store store.Store, step model.Step, state rpc.State) (*model.Step, error) {
step.Stopped = state.Finished
func UpdateStepStatusToDone(store store.Store, step model.Step, state rpc.StepState) (*model.Step, error) {
step.Finished = state.Finished
step.Error = state.Error
step.ExitCode = state.ExitCode
if state.Started == 0 {
@@ -75,9 +75,9 @@ func UpdateStepStatusToDone(store store.Store, step model.Step, state rpc.State)
func UpdateStepToStatusKilled(store store.Store, step model.Step) (*model.Step, error) {
step.State = model.StatusKilled
step.Stopped = time.Now().Unix()
step.Finished = time.Now().Unix()
if step.Started == 0 {
step.Started = step.Stopped
step.Started = step.Finished
}
step.ExitCode = pipeline.ExitCodeKilled
return &step, store.StepUpdate(&step)

View File

@@ -41,7 +41,7 @@ func TestUpdateStepStatusNotExited(t *testing.T) {
step := &model.Step{}
// advertised step status
state := rpc.State{
state := rpc.StepState{
Started: int64(42),
Exited: false,
// Dummy data
@@ -54,7 +54,7 @@ func TestUpdateStepStatusNotExited(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, model.StatusRunning, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 0, step.Stopped)
assert.EqualValues(t, 0, step.Finished)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "", step.Error)
}
@@ -63,10 +63,10 @@ func TestUpdateStepStatusNotExitedButStopped(t *testing.T) {
t.Parallel()
// step in db before update
step := &model.Step{Started: 42, Stopped: 64, State: model.StatusKilled}
step := &model.Step{Started: 42, Finished: 64, State: model.StatusKilled}
// advertised step status
state := rpc.State{
state := rpc.StepState{
Exited: false,
// Dummy data
Finished: int64(1),
@@ -78,7 +78,7 @@ func TestUpdateStepStatusNotExitedButStopped(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, model.StatusKilled, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 64, step.Stopped)
assert.EqualValues(t, 64, step.Finished)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "", step.Error)
}
@@ -90,7 +90,7 @@ func TestUpdateStepStatusExited(t *testing.T) {
step := &model.Step{Started: 42}
// advertised step status
state := rpc.State{
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
@@ -102,7 +102,7 @@ func TestUpdateStepStatusExited(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, model.StatusKilled, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 34, step.Finished)
assert.EqualValues(t, pipeline.ExitCodeKilled, step.ExitCode)
assert.EqualValues(t, "an error", step.Error)
}
@@ -114,7 +114,7 @@ func TestUpdateStepStatusExitedButNot137(t *testing.T) {
step := &model.Step{Started: 42}
// advertised step status
state := rpc.State{
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
@@ -125,7 +125,7 @@ func TestUpdateStepStatusExitedButNot137(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, model.StatusFailure, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 34, step.Finished)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "an error", step.Error)
}
@@ -134,7 +134,7 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
t.Parallel()
// advertised step status
state := rpc.State{
state := rpc.StepState{
Started: int64(42),
Exited: true,
Finished: int64(34),
@@ -149,10 +149,10 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
assert.Equal(t, 1, step.ExitCode)
}
func TestUpdateStepPToStatusStarted(t *testing.T) {
func TestUpdateStepToStatusStarted(t *testing.T) {
t.Parallel()
state := rpc.State{Started: int64(42)}
state := rpc.StepState{Started: int64(42)}
step, _ := UpdateStepToStatusStarted(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusRunning, step.State)
@@ -165,7 +165,7 @@ func TestUpdateStepToStatusSkipped(t *testing.T) {
step, _ := UpdateStepToStatusSkipped(mockStoreStep(t), model.Step{}, int64(1))
assert.Equal(t, model.StatusSkipped, step.State)
assert.EqualValues(t, 0, step.Stopped)
assert.EqualValues(t, 0, step.Finished)
}
func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
@@ -178,20 +178,20 @@ func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
step, _ = UpdateStepToStatusSkipped(mockStoreStep(t), *step, int64(1))
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 1, step.Stopped)
assert.EqualValues(t, 1, step.Finished)
}
func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
t.Parallel()
state := rpc.State{
state := rpc.StepState{
Finished: int64(34),
}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusSkipped, step.State)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 34, step.Finished)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
@@ -199,7 +199,7 @@ func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
t.Parallel()
state := rpc.State{
state := rpc.StepState{
Started: int64(42),
Finished: int64(34),
}
@@ -207,7 +207,7 @@ func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
assert.Equal(t, model.StatusSuccess, step.State)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 34, step.Finished)
assert.Empty(t, step.Error)
assert.Equal(t, 0, step.ExitCode)
}
@@ -215,7 +215,7 @@ func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
t.Parallel()
state := rpc.State{Error: "an error"}
state := rpc.StepState{Error: "an error"}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
@@ -225,7 +225,7 @@ func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
t.Parallel()
state := rpc.State{ExitCode: 43}
state := rpc.StepState{ExitCode: 43}
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
@@ -240,8 +240,8 @@ func TestUpdateStepToStatusKilledStarted(t *testing.T) {
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{})
assert.Equal(t, model.StatusKilled, step.State)
assert.LessOrEqual(t, now, step.Stopped)
assert.Equal(t, step.Stopped, step.Started)
assert.LessOrEqual(t, now, step.Finished)
assert.Equal(t, step.Finished, step.Started)
assert.Equal(t, 137, step.ExitCode)
}

View File

@@ -20,7 +20,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)
func UpdateWorkflowToStatusStarted(store store.Store, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
func UpdateWorkflowStatusToRunning(store store.Store, workflow model.Workflow, state rpc.WorkflowState) (*model.Workflow, error) {
workflow.Started = state.Started
workflow.State = model.StatusRunning
return &workflow, store.WorkflowUpdate(&workflow)
@@ -31,8 +31,8 @@ func UpdateWorkflowToStatusSkipped(store store.Store, workflow model.Workflow) (
return &workflow, store.WorkflowUpdate(&workflow)
}
func UpdateWorkflowStatusToDone(store store.Store, workflow model.Workflow, state rpc.State) (*model.Workflow, error) {
workflow.Stopped = state.Finished
func UpdateWorkflowStatusToDone(store store.Store, workflow model.Workflow, state rpc.WorkflowState) (*model.Workflow, error) {
workflow.Finished = state.Finished
workflow.Error = state.Error
if state.Started == 0 {
workflow.State = model.StatusSkipped