mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-02-13 21:00:00 +00:00
Co-authored-by: pnkcaht <samzoovsk19@gmail.com> Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com> Co-authored-by: Lauris B <lauris@nix.lv>
100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
// Copyright 2022 Woodpecker Authors
|
|
// Copyright 2019 mhmxs
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/rpc"
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/model"
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/store"
|
|
)
|
|
|
|
// UpdateStepStatus updates step status based on agent reports via RPC.
|
|
func UpdateStepStatus(store store.Store, step *model.Step, state rpc.StepState) error {
|
|
log.Debug().Str("StepUUID", step.UUID).Msgf("Update step %#v state %#v", *step, state)
|
|
|
|
switch step.State {
|
|
case model.StatusPending:
|
|
// Transition from pending to running when started
|
|
if state.Finished == 0 {
|
|
step.State = model.StatusRunning
|
|
}
|
|
step.Started = state.Started
|
|
if step.Started == 0 {
|
|
step.Started = time.Now().Unix()
|
|
}
|
|
|
|
// Handle direct transition to finished if step setup error happened
|
|
if state.Exited || state.Error != "" {
|
|
step.Finished = state.Finished
|
|
if step.Finished == 0 {
|
|
step.Finished = time.Now().Unix()
|
|
}
|
|
step.ExitCode = state.ExitCode
|
|
step.Error = state.Error
|
|
|
|
if state.ExitCode == 0 && state.Error == "" {
|
|
step.State = model.StatusSuccess
|
|
} else {
|
|
step.State = model.StatusFailure
|
|
}
|
|
}
|
|
|
|
case model.StatusRunning:
|
|
// Already running, check if it finished
|
|
if state.Exited || state.Error != "" {
|
|
step.Finished = state.Finished
|
|
if step.Finished == 0 {
|
|
step.Finished = time.Now().Unix()
|
|
}
|
|
step.ExitCode = state.ExitCode
|
|
step.Error = state.Error
|
|
|
|
if state.ExitCode == 0 && state.Error == "" {
|
|
step.State = model.StatusSuccess
|
|
} else {
|
|
step.State = model.StatusFailure
|
|
}
|
|
}
|
|
|
|
default:
|
|
return fmt.Errorf("step has state %s and does not expect rpc state updates", step.State)
|
|
}
|
|
|
|
// Handle cancellation across both cases
|
|
if state.Canceled && step.State != model.StatusKilled {
|
|
step.State = model.StatusKilled
|
|
if step.Finished == 0 {
|
|
step.Finished = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
return store.StepUpdate(step)
|
|
}
|
|
|
|
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.Finished = finished
|
|
}
|
|
return &step, store.StepUpdate(&step)
|
|
}
|