2021-11-13 19:18:06 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-03-28 08:53:06 +00:00
|
|
|
package model
|
|
|
|
|
2019-06-06 07:41:42 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2017-03-28 08:53:06 +00:00
|
|
|
// ProcStore persists process information to storage.
|
|
|
|
type ProcStore interface {
|
2017-04-01 11:17:04 +00:00
|
|
|
ProcLoad(int64) (*Proc, error)
|
2017-03-28 08:53:06 +00:00
|
|
|
ProcFind(*Build, int) (*Proc, error)
|
|
|
|
ProcChild(*Build, int, string) (*Proc, error)
|
|
|
|
ProcList(*Build) ([]*Proc, error)
|
|
|
|
ProcCreate([]*Proc) error
|
|
|
|
ProcUpdate(*Proc) error
|
2017-04-04 10:50:15 +00:00
|
|
|
ProcClear(*Build) error
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Proc represents a process in the build pipeline.
|
2017-04-01 11:17:04 +00:00
|
|
|
// swagger:model proc
|
2017-03-28 08:53:06 +00:00
|
|
|
type Proc struct {
|
2021-11-13 19:18:06 +00:00
|
|
|
ID int64 `json:"id" xorm:"pk autoincr 'proc_id'"`
|
|
|
|
BuildID int64 `json:"build_id" xorm:"UNIQUE(s) INDEX 'proc_build_id'"`
|
|
|
|
PID int `json:"pid" xorm:"UNIQUE(s) 'proc_pid'"`
|
|
|
|
PPID int `json:"ppid" xorm:"proc_ppid"`
|
|
|
|
PGID int `json:"pgid" xorm:"proc_pgid"`
|
|
|
|
Name string `json:"name" xorm:"proc_name"`
|
2021-11-22 11:55:13 +00:00
|
|
|
State StatusValue `json:"state" xorm:"proc_state"`
|
2021-11-13 19:18:06 +00:00
|
|
|
Error string `json:"error,omitempty" xorm:"VARCHAR(500) proc_error"`
|
|
|
|
ExitCode int `json:"exit_code" xorm:"proc_exit_code"`
|
|
|
|
Started int64 `json:"start_time,omitempty" xorm:"proc_started"`
|
|
|
|
Stopped int64 `json:"end_time,omitempty" xorm:"proc_stopped"`
|
|
|
|
Machine string `json:"machine,omitempty" xorm:"proc_machine"`
|
|
|
|
Platform string `json:"platform,omitempty" xorm:"proc_platform"`
|
|
|
|
Environ map[string]string `json:"environ,omitempty" xorm:"json 'proc_environ'"`
|
|
|
|
Children []*Proc `json:"children,omitempty" xorm:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName return database table name for xorm
|
|
|
|
func (Proc) TableName() string {
|
|
|
|
return "procs"
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Running returns true if the process state is pending or running.
|
|
|
|
func (p *Proc) Running() bool {
|
|
|
|
return p.State == StatusPending || p.State == StatusRunning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failing returns true if the process state is failed, killed or error.
|
|
|
|
func (p *Proc) Failing() bool {
|
|
|
|
return p.State == StatusError || p.State == StatusKilled || p.State == StatusFailure
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:02:49 +00:00
|
|
|
// IsParent returns true if the process is a parent process.
|
|
|
|
func (p *Proc) IsParent() bool {
|
|
|
|
return p.PPID == 0
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:38:39 +00:00
|
|
|
// IsMultiPipeline checks if proc list contain more than one parent proc
|
|
|
|
func IsMultiPipeline(procs []*Proc) bool {
|
|
|
|
c := 0
|
|
|
|
for _, proc := range procs {
|
|
|
|
if proc.IsParent() {
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
if c > 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-28 08:53:06 +00:00
|
|
|
// Tree creates a process tree from a flat process list.
|
2021-12-08 22:36:23 +00:00
|
|
|
func Tree(procs []*Proc) ([]*Proc, error) {
|
2019-06-06 07:41:42 +00:00
|
|
|
var nodes []*Proc
|
2021-12-31 16:43:09 +00:00
|
|
|
|
|
|
|
// init parent nodes
|
|
|
|
for i := range procs {
|
|
|
|
if procs[i].IsParent() {
|
|
|
|
nodes = append(nodes, procs[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign children to parrents
|
|
|
|
for i := range procs {
|
|
|
|
if !procs[i].IsParent() {
|
|
|
|
parent, err := findNode(nodes, procs[i].PPID)
|
2021-12-08 22:36:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-31 16:43:09 +00:00
|
|
|
parent.Children = append(parent.Children, procs[i])
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-31 16:43:09 +00:00
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
return nodes, nil
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
2019-06-06 07:41:42 +00:00
|
|
|
|
2022-01-31 14:38:39 +00:00
|
|
|
// BuildStatus determine build status based on corresponding proc list
|
|
|
|
func BuildStatus(procs []*Proc) StatusValue {
|
|
|
|
status := StatusSuccess
|
|
|
|
|
|
|
|
for _, p := range procs {
|
|
|
|
if p.IsParent() && p.Failing() {
|
|
|
|
status = p.State
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsThereRunningStage determine if it contains procs running or pending to run
|
|
|
|
// TODO: return false based on depends_on (https://github.com/woodpecker-ci/woodpecker/pull/730#discussion_r795681697)
|
|
|
|
func IsThereRunningStage(procs []*Proc) bool {
|
|
|
|
for _, p := range procs {
|
|
|
|
if p.IsParent() {
|
|
|
|
if p.Running() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-06 07:41:42 +00:00
|
|
|
func findNode(nodes []*Proc, pid int) (*Proc, error) {
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.PID == pid {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Corrupt proc structure")
|
|
|
|
}
|