tendermint/process/process.go

95 lines
2.0 KiB
Go
Raw Normal View History

package process
import (
2015-04-10 02:12:17 -07:00
"bytes"
"fmt"
"io"
"os"
"os/exec"
"time"
)
2015-04-10 02:12:17 -07:00
func makeFile(prefix string) (string, *os.File) {
now := time.Now()
2015-04-10 02:12:17 -07:00
path := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST"))
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
2015-04-10 02:12:17 -07:00
return path, file
}
type Process struct {
2015-04-10 02:12:17 -07:00
Label string
ExecPath string
2015-04-16 09:46:35 -07:00
Pid int
2015-04-10 02:12:17 -07:00
StartTime time.Time
2015-04-16 09:46:35 -07:00
EndTime time.Time
2015-04-10 02:12:17 -07:00
OutputPath string
Cmd *exec.Cmd `json:"-"`
ExitState *os.ProcessState `json:"-"`
OutputFile *os.File `json:"-"`
}
const (
ProcessModeStd = iota
ProcessModeDaemon
)
2015-04-08 11:35:17 -07:00
// execPath: command name
// args: args to command. (should not include name)
2015-04-16 09:46:35 -07:00
func Create(mode int, label string, execPath string, args []string, input string) (*Process, error) {
2015-04-10 02:12:17 -07:00
outPath, outFile := makeFile(label)
2015-04-08 11:35:17 -07:00
cmd := exec.Command(execPath, args...)
switch mode {
case ProcessModeStd:
2015-04-10 02:12:17 -07:00
cmd.Stdout = io.MultiWriter(os.Stdout, outFile)
cmd.Stderr = io.MultiWriter(os.Stderr, outFile)
cmd.Stdin = nil
case ProcessModeDaemon:
2015-04-10 02:12:17 -07:00
cmd.Stdout = outFile
cmd.Stderr = outFile
cmd.Stdin = nil
}
2015-04-10 02:12:17 -07:00
if input != "" {
cmd.Stdin = bytes.NewReader([]byte(input))
}
if err := cmd.Start(); err != nil {
2015-04-16 09:46:35 -07:00
return nil, err
} else {
fmt.Printf("Success!")
}
2015-04-16 09:46:35 -07:00
proc := &Process{
2015-04-10 02:12:17 -07:00
Label: label,
ExecPath: execPath,
2015-04-16 09:46:35 -07:00
Pid: cmd.Process.Pid,
2015-04-10 02:12:17 -07:00
StartTime: time.Now(),
OutputPath: outPath,
Cmd: cmd,
ExitState: nil,
OutputFile: outFile,
}
2015-04-16 09:46:35 -07:00
go func() {
Wait(proc)
proc.EndTime = time.Now() // TODO make this goroutine-safe
}()
return proc, nil
}
2015-04-08 11:35:17 -07:00
func Wait(proc *Process) error {
exitErr := proc.Cmd.Wait()
if exitErr != nil {
2015-04-08 11:35:17 -07:00
fmt.Printf("Process exit: %v\n", exitErr)
proc.ExitState = exitErr.(*exec.ExitError).ProcessState
}
return exitErr
}
func Stop(proc *Process, kill bool) error {
if kill {
return proc.Cmd.Process.Kill()
} else {
return proc.Cmd.Process.Signal(os.Interrupt)
}
}