health: add more ci cases and fix bugs

This commit is contained in:
fatedier
2018-12-09 21:56:46 +08:00
parent 08c17c3247
commit aea9f9fbcc
14 changed files with 409 additions and 66 deletions

View File

@@ -1,22 +1,29 @@
package util
import (
"bytes"
"context"
"os/exec"
)
type Process struct {
cmd *exec.Cmd
cancel context.CancelFunc
cmd *exec.Cmd
cancel context.CancelFunc
errorOutput *bytes.Buffer
beforeStopHandler func()
}
func NewProcess(path string, params []string) *Process {
ctx, cancel := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, path, params...)
return &Process{
p := &Process{
cmd: cmd,
cancel: cancel,
}
p.errorOutput = bytes.NewBufferString("")
cmd.Stderr = p.errorOutput
return p
}
func (p *Process) Start() error {
@@ -24,6 +31,17 @@ func (p *Process) Start() error {
}
func (p *Process) Stop() error {
if p.beforeStopHandler != nil {
p.beforeStopHandler()
}
p.cancel()
return p.cmd.Wait()
}
func (p *Process) ErrorOutput() string {
return p.errorOutput.String()
}
func (p *Process) SetBeforeStopHandler(fn func()) {
p.beforeStopHandler = fn
}