2015-04-07 11:44:25 -07:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
2015-04-10 02:12:17 -07:00
|
|
|
"bytes"
|
2015-04-07 11:44:25 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-04-10 02:12:17 -07:00
|
|
|
func makeFile(prefix string) (string, *os.File) {
|
2015-04-07 11:44:25 -07:00
|
|
|
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)
|
2015-04-07 11:44:25 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-10 02:12:17 -07:00
|
|
|
return path, file
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
2015-04-16 10:54:07 -07:00
|
|
|
WaitCh chan struct{} `json:"-"`
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2015-04-07 11:44:25 -07:00
|
|
|
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)
|
2015-04-07 11:44:25 -07:00
|
|
|
cmd.Stdin = nil
|
|
|
|
case ProcessModeDaemon:
|
2015-04-10 02:12:17 -07:00
|
|
|
cmd.Stdout = outFile
|
|
|
|
cmd.Stderr = outFile
|
2015-04-07 11:44:25 -07:00
|
|
|
cmd.Stdin = nil
|
|
|
|
}
|
2015-04-10 02:12:17 -07:00
|
|
|
if input != "" {
|
|
|
|
cmd.Stdin = bytes.NewReader([]byte(input))
|
|
|
|
}
|
2015-04-07 11:44:25 -07:00
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-16 09:46:35 -07:00
|
|
|
return nil, err
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
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 10:54:07 -07:00
|
|
|
WaitCh: make(chan struct{}),
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
2015-04-16 09:46:35 -07:00
|
|
|
go func() {
|
2015-04-16 10:54:07 -07:00
|
|
|
err := proc.Cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Process exit: %v\n", err)
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
proc.ExitState = exitError.ProcessState
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 09:46:35 -07:00
|
|
|
proc.EndTime = time.Now() // TODO make this goroutine-safe
|
2015-04-16 10:54:07 -07:00
|
|
|
close(proc.WaitCh)
|
2015-04-16 09:46:35 -07:00
|
|
|
}()
|
|
|
|
return proc, nil
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
|
|
|
|
2015-04-08 11:35:17 -07:00
|
|
|
func Stop(proc *Process, kill bool) error {
|
|
|
|
if kill {
|
|
|
|
return proc.Cmd.Process.Kill()
|
|
|
|
} else {
|
|
|
|
return proc.Cmd.Process.Signal(os.Interrupt)
|
2015-04-07 11:44:25 -07:00
|
|
|
}
|
|
|
|
}
|