woodpecker/server/model/proc.go

83 lines
3 KiB
Go
Raw Normal View History

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
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 {
ID int64 `json:"id" meddler:"proc_id,pk"`
BuildID int64 `json:"build_id" meddler:"proc_build_id"`
PID int `json:"pid" meddler:"proc_pid"`
PPID int `json:"ppid" meddler:"proc_ppid"`
PGID int `json:"pgid" meddler:"proc_pgid"`
Name string `json:"name" meddler:"proc_name"`
State string `json:"state" meddler:"proc_state"`
Error string `json:"error,omitempty" meddler:"proc_error"`
ExitCode int `json:"exit_code" meddler:"proc_exit_code"`
Started int64 `json:"start_time,omitempty" meddler:"proc_started"`
Stopped int64 `json:"end_time,omitempty" meddler:"proc_stopped"`
Machine string `json:"machine,omitempty" meddler:"proc_machine"`
Platform string `json:"platform,omitempty" meddler:"proc_platform"`
Environ map[string]string `json:"environ,omitempty" meddler:"proc_environ,json"`
Children []*Proc `json:"children,omitempty" meddler:"-"`
}
// 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
}
// Tree creates a process tree from a flat process list.
func Tree(procs []*Proc) []*Proc {
2019-06-06 07:41:42 +00:00
var nodes []*Proc
2017-03-28 08:53:06 +00:00
for _, proc := range procs {
if proc.PPID == 0 {
nodes = append(nodes, proc)
} else {
2019-06-06 07:41:42 +00:00
parent, _ := findNode(nodes, proc.PPID)
2017-03-28 08:53:06 +00:00
parent.Children = append(parent.Children, proc)
}
}
return nodes
}
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")
}