mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-20 07:26:34 +00:00
Create separate Go file for proc status updates
This commit is contained in:
parent
53258bf989
commit
db4876aabc
3 changed files with 104 additions and 64 deletions
|
@ -19,6 +19,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -185,14 +186,10 @@ func DeleteBuild(c *gin.Context) {
|
|||
continue
|
||||
}
|
||||
|
||||
proc.State = model.StatusKilled
|
||||
proc.Stopped = time.Now().Unix()
|
||||
if proc.Started == 0 {
|
||||
proc.Started = proc.Stopped
|
||||
}
|
||||
proc.ExitCode = 137
|
||||
// TODO cancel child procs
|
||||
store.FromContext(c).ProcUpdate(proc)
|
||||
if _, err = UpdateProcToStatusKilled(store.FromContext(c), *proc); err != nil {
|
||||
log.Printf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
||||
}
|
||||
|
||||
Config.Services.Queue.Error(context.Background(), fmt.Sprint(proc.ID), queue.ErrCancel)
|
||||
cancelled = true
|
||||
|
@ -235,17 +232,12 @@ func ZombieKill(c *gin.Context) {
|
|||
|
||||
for _, proc := range procs {
|
||||
if proc.Running() {
|
||||
proc.State = model.StatusKilled
|
||||
proc.ExitCode = 137
|
||||
proc.Stopped = time.Now().Unix()
|
||||
if proc.Started == 0 {
|
||||
proc.Started = proc.Stopped
|
||||
if _, err := UpdateProcToStatusKilled(store.FromContext(c), *proc); err != nil {
|
||||
log.Printf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, proc := range procs {
|
||||
} else {
|
||||
store.FromContext(c).ProcUpdate(proc)
|
||||
}
|
||||
Config.Services.Queue.Error(context.Background(), fmt.Sprint(proc.ID), queue.ErrCancel)
|
||||
}
|
||||
|
||||
|
|
89
server/procStatus.go
Normal file
89
server/procStatus.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
// 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 server
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/rpc"
|
||||
"github.com/laszlocph/woodpecker/model"
|
||||
)
|
||||
|
||||
type UpdateProcStore interface {
|
||||
ProcUpdate(*model.Proc) error
|
||||
}
|
||||
|
||||
func UpdateProcStatus(store UpdateProcStore, proc model.Proc, state rpc.State, started int64) (*model.Proc, error) {
|
||||
if state.Exited {
|
||||
proc.Stopped = state.Finished
|
||||
proc.ExitCode = state.ExitCode
|
||||
proc.Error = state.Error
|
||||
proc.State = model.StatusSuccess
|
||||
if state.ExitCode != 0 || state.Error != "" {
|
||||
proc.State = model.StatusFailure
|
||||
}
|
||||
if state.ExitCode == 137 {
|
||||
proc.State = model.StatusKilled
|
||||
}
|
||||
} else {
|
||||
proc.Started = state.Started
|
||||
proc.State = model.StatusRunning
|
||||
}
|
||||
|
||||
if proc.Started == 0 && proc.Stopped != 0 {
|
||||
proc.Started = started
|
||||
}
|
||||
return &proc, store.ProcUpdate(&proc)
|
||||
}
|
||||
|
||||
func UpdateProcToStatusStarted(store UpdateProcStore, proc model.Proc, state rpc.State) (*model.Proc, error) {
|
||||
proc.Started = state.Started
|
||||
proc.State = model.StatusRunning
|
||||
return &proc, store.ProcUpdate(&proc)
|
||||
}
|
||||
|
||||
func UpdateProcToStatusSkipped(store UpdateProcStore, proc model.Proc, stopped int64) (*model.Proc, error) {
|
||||
proc.State = model.StatusSkipped
|
||||
if proc.Started != 0 {
|
||||
proc.State = model.StatusSuccess // for deamons that are killed
|
||||
proc.Stopped = stopped
|
||||
}
|
||||
return &proc, store.ProcUpdate(&proc)
|
||||
}
|
||||
|
||||
func UpdateProcStatusToDone(store UpdateProcStore, proc model.Proc, state rpc.State) (*model.Proc, error) {
|
||||
proc.Stopped = state.Finished
|
||||
proc.Error = state.Error
|
||||
proc.ExitCode = state.ExitCode
|
||||
if state.Started == 0 {
|
||||
proc.State = model.StatusSkipped
|
||||
} else {
|
||||
proc.State = model.StatusSuccess
|
||||
}
|
||||
if proc.ExitCode != 0 || proc.Error != "" {
|
||||
proc.State = model.StatusFailure
|
||||
}
|
||||
return &proc, store.ProcUpdate(&proc)
|
||||
}
|
||||
|
||||
func UpdateProcToStatusKilled(store UpdateProcStore, proc model.Proc) (*model.Proc, error) {
|
||||
proc.State = model.StatusKilled
|
||||
proc.Stopped = time.Now().Unix()
|
||||
if proc.Started == 0 {
|
||||
proc.Started = proc.Stopped
|
||||
}
|
||||
proc.ExitCode = 137
|
||||
return &proc, store.ProcUpdate(&proc)
|
||||
}
|
|
@ -179,27 +179,7 @@ func (s *RPC) Update(c context.Context, id string, state rpc.State) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if state.Exited {
|
||||
proc.Stopped = state.Finished
|
||||
proc.ExitCode = state.ExitCode
|
||||
proc.Error = state.Error
|
||||
proc.State = model.StatusSuccess
|
||||
if state.ExitCode != 0 || state.Error != "" {
|
||||
proc.State = model.StatusFailure
|
||||
}
|
||||
if state.ExitCode == 137 {
|
||||
proc.State = model.StatusKilled
|
||||
}
|
||||
} else {
|
||||
proc.Started = state.Started
|
||||
proc.State = model.StatusRunning
|
||||
}
|
||||
|
||||
if proc.Started == 0 && proc.Stopped != 0 {
|
||||
proc.Started = build.Started
|
||||
}
|
||||
|
||||
if err := s.store.ProcUpdate(proc); err != nil {
|
||||
if proc, err = UpdateProcStatus(s.store, *proc, state, build.Started); err != nil {
|
||||
log.Printf("error: rpc.update: cannot update proc: %s", err)
|
||||
}
|
||||
|
||||
|
@ -346,9 +326,8 @@ func (s *RPC) Init(c context.Context, id string, state rpc.State) error {
|
|||
s.pubsub.Publish(c, "topic/events", message)
|
||||
}()
|
||||
|
||||
proc.Started = state.Started
|
||||
proc.State = model.StatusRunning
|
||||
return s.store.ProcUpdate(proc)
|
||||
_, err = UpdateProcToStatusStarted(s.store, *proc, state)
|
||||
return err
|
||||
}
|
||||
|
||||
// Done implements the rpc.Done function
|
||||
|
@ -376,7 +355,9 @@ func (s *RPC) Done(c context.Context, id string, state rpc.State) error {
|
|||
return err
|
||||
}
|
||||
|
||||
s.updateProcState(proc, state)
|
||||
if proc, err = UpdateProcStatusToDone(s.store, *proc, state); err != nil {
|
||||
log.Printf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
||||
}
|
||||
|
||||
var queueErr error
|
||||
if proc.Failing() {
|
||||
|
@ -440,32 +421,10 @@ func (s *RPC) Log(c context.Context, id string, line *rpc.Line) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *RPC) updateProcState(proc *model.Proc, state rpc.State) {
|
||||
proc.Stopped = state.Finished
|
||||
proc.Error = state.Error
|
||||
proc.ExitCode = state.ExitCode
|
||||
if state.Started == 0 {
|
||||
proc.State = model.StatusSkipped
|
||||
} else {
|
||||
proc.State = model.StatusSuccess
|
||||
}
|
||||
if proc.ExitCode != 0 || proc.Error != "" {
|
||||
proc.State = model.StatusFailure
|
||||
}
|
||||
if err := s.store.ProcUpdate(proc); err != nil {
|
||||
log.Printf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RPC) completeChildrenIfParentCompleted(procs []*model.Proc, completedProc *model.Proc) {
|
||||
for _, p := range procs {
|
||||
if p.Running() && p.PPID == completedProc.PID {
|
||||
p.State = model.StatusSkipped
|
||||
if p.Started != 0 {
|
||||
p.State = model.StatusSuccess // for deamons that are killed
|
||||
p.Stopped = completedProc.Stopped
|
||||
}
|
||||
if err := s.store.ProcUpdate(p); err != nil {
|
||||
if _, err := UpdateProcToStatusSkipped(s.store, *p, completedProc.Stopped); err != nil {
|
||||
log.Printf("error: done: cannot update proc_id %d child state: %s", p.ID, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue