RPC refactor to separate core from core_client and the rest of RPC.

Other random changes.
This commit is contained in:
Jae Kwon
2015-04-07 11:44:25 -07:00
parent f271ab7256
commit cc18136af8
27 changed files with 1015 additions and 414 deletions

61
process/process.go Normal file
View File

@ -0,0 +1,61 @@
package process
import (
"fmt"
"io"
"os"
"os/exec"
"time"
)
func makeFile(prefix string) *os.File {
now := time.Now()
filename := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST"))
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
return f
}
type Process struct {
Cmd *exec.Cmd
Output *os.File
}
const (
ProcessModeStd = iota
ProcessModeDaemon
)
func CreateProcess(mode int, name string, args ...string) *Process {
out := makeFile(name)
cmd := exec.Command(name, args...)
switch mode {
case ProcessModeStd:
cmd.Stdout = io.MultiWriter(os.Stdout, out)
cmd.Stderr = io.MultiWriter(os.Stderr, out)
cmd.Stdin = nil
case ProcessModeDaemon:
cmd.Stdout = out
cmd.Stderr = out
cmd.Stdin = nil
}
if err := cmd.Start(); err != nil {
fmt.Printf("Failed to run command. %v\n", err)
return nil
} else {
fmt.Printf("Success!")
}
return &Process{
Cmd: cmd,
Output: out,
}
}
func Watch(proc *Process) {
exitErr := proc.Cmd.Wait()
if exitErr != nil {
fmt.Println("%v", exitErr)
}
}