Browse Source

refactor process.Process to take files

pull/157/head
Jae Kwon 9 years ago
parent
commit
5102f7a9cb
3 changed files with 26 additions and 44 deletions
  1. +18
    -4
      cmd/barak/main.go
  2. +1
    -2
      cmd/debora/main.go
  3. +7
    -38
      process/process.go

+ 18
- 4
cmd/barak/main.go View File

@ -5,21 +5,23 @@ package main
// TODO: Nonrepudiable command log
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
"time"
"github.com/tendermint/tendermint/wire"
. "github.com/tendermint/tendermint/cmd/barak/types"
. "github.com/tendermint/tendermint/common"
cfg "github.com/tendermint/tendermint/config"
pcm "github.com/tendermint/tendermint/process"
"github.com/tendermint/tendermint/rpc/server"
"github.com/tendermint/tendermint/wire"
)
const BarakVersion = "0.0.1"
@ -152,8 +154,13 @@ func StartProcess(wait bool, label string, execPath string, args []string, input
if err != nil {
return nil, fmt.Errorf("Failed to create outputs dir: %v", err)
}
inFile := bytes.NewReader([]byte(input))
outPath := Fmt("%v/outputs/%v_%v.out", barak_.RootDir(), label, time.Now().Format("2006_01_02_15_04_05_MST"))
proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input, outPath)
outFile, err := OpenAutoFile(outPath)
if err != nil {
return nil, err
}
proc, err := pcm.Create(label, execPath, args, inFile, outFile)
if err != nil {
return nil, err
}
@ -161,7 +168,14 @@ func StartProcess(wait bool, label string, execPath string, args []string, input
if wait {
<-proc.WaitCh
output := pcm.ReadOutput(proc)
// read output from outPath
outputBytes, err := ioutil.ReadFile(outPath)
if err != nil {
fmt.Sprintf("ERROR READING OUTPUT: %v", err)
}
output := string(outputBytes)
// fmt.Println("Read output", output)
if proc.ExitState == nil {
return &ResponseStartProcess{
@ -260,7 +274,7 @@ func ServeFileHandler(w http.ResponseWriter, req *http.Request) {
http.Error(w, Fmt("Unknown process label: %v", path), 400)
return
}
path = proc.OutputPath
path = proc.OutputFile.(*os.File).Name()
}
file, err := os.Open(path)
if err != nil {


+ 1
- 2
cmd/debora/main.go View File

@ -11,10 +11,10 @@ import (
"sync"
acm "github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/wire"
btypes "github.com/tendermint/tendermint/cmd/barak/types"
. "github.com/tendermint/tendermint/common"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/wire"
)
func remoteNick(remote string) string {
@ -268,7 +268,6 @@ func cliListProcesses(c *cli.Context) {
endTimeStr := proc.EndTime.String()
fmt.Printf(", stopped at %v\n", Yellow(endTimeStr))
}
fmt.Printf(" stdout/stderr goes to %v\n", proc.OutputPath)
}
}
}(remote)


+ 7
- 38
process/process.go View File

@ -1,15 +1,11 @@
package process
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"time"
. "github.com/tendermint/tendermint/common"
)
type Process struct {
@ -19,39 +15,20 @@ type Process struct {
Pid int
StartTime time.Time
EndTime time.Time
OutputPath string
Cmd *exec.Cmd `json:"-"`
ExitState *os.ProcessState `json:"-"`
OutputFile *AutoFile `json:"-"`
InputFile io.Reader `json:"-"`
OutputFile io.WriteCloser `json:"-"`
WaitCh chan struct{} `json:"-"`
}
const (
ProcessModeStd = iota
ProcessModeDaemon
)
// execPath: command name
// args: args to command. (should not include name)
func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) {
outFile, err := OpenAutoFile(outPath)
if err != nil {
return nil, err
}
func Create(label string, execPath string, args []string, inFile io.Reader, outFile io.WriteCloser) (*Process, error) {
cmd := exec.Command(execPath, args...)
switch mode {
case ProcessModeStd:
cmd.Stdout = io.MultiWriter(os.Stdout, outFile)
cmd.Stderr = io.MultiWriter(os.Stderr, outFile)
cmd.Stdin = nil
case ProcessModeDaemon:
cmd.Stdout = outFile
cmd.Stderr = outFile
cmd.Stdin = nil
}
if input != "" {
cmd.Stdin = bytes.NewReader([]byte(input))
}
cmd.Stdout = outFile
cmd.Stderr = outFile
cmd.Stdin = inFile
if err := cmd.Start(); err != nil {
return nil, err
}
@ -61,9 +38,9 @@ func Create(mode int, label string, execPath string, args []string, input string
Args: args,
Pid: cmd.Process.Pid,
StartTime: time.Now(),
OutputPath: outPath,
Cmd: cmd,
ExitState: nil,
InputFile: inFile,
OutputFile: outFile,
WaitCh: make(chan struct{}),
}
@ -85,14 +62,6 @@ func Create(mode int, label string, execPath string, args []string, input string
return proc, nil
}
func ReadOutput(proc *Process) string {
output, err := ioutil.ReadFile(proc.OutputPath)
if err != nil {
return fmt.Sprintf("ERROR READING OUTPUT: %v", err)
}
return string(output)
}
func Stop(proc *Process, kill bool) error {
defer proc.OutputFile.Close()
if kill {


Loading…
Cancel
Save