Browse Source

Run process

pull/55/head
Jae Kwon 10 years ago
parent
commit
9ec6258ed0
5 changed files with 69 additions and 27 deletions
  1. +8
    -2
      cmd/barak/main.go
  2. +5
    -0
      cmd/debora/commands.go
  3. +42
    -21
      cmd/debora/main.go
  4. +11
    -4
      process/process.go
  5. +3
    -0
      rpc/client.go

+ 8
- 2
cmd/barak/main.go View File

@ -155,9 +155,14 @@ func RunProcess(wait bool, label string, execPath string, args []string, input s
}
// Otherwise, create one.
proc := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input)
barak.processes[label] = proc
proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input)
if err == nil {
barak.processes[label] = proc
}
barak.mtx.Unlock()
if err != nil {
return nil, err
}
if wait {
exitErr := pcm.Wait(proc)
@ -183,6 +188,7 @@ func StopProcess(label string, kill bool) (*ResponseStopProcess, error) {
func ListProcesses() (*ResponseListProcesses, error) {
var procs = []*pcm.Process{}
barak.mtx.Lock()
fmt.Println("Processes: %v", barak.processes)
for _, proc := range barak.processes {
procs = append(procs, proc)
}


+ 5
- 0
cmd/debora/commands.go View File

@ -9,6 +9,11 @@ import (
"github.com/tendermint/tendermint/rpc"
)
// These are convenience functions for a single developer.
// When multiple are involved, the workflow is different.
// (First the command(s) are signed by all validators,
// and then it is broadcast).
func RunProcess(privKey acm.PrivKey, remote string, command btypes.CommandRunProcess) (response btypes.ResponseRunProcess, err error) {
nonce, err := GetNonce(remote)
if err != nil {


+ 42
- 21
cmd/debora/main.go View File

@ -26,6 +26,15 @@ var (
Value: "privkey",
Usage: "file containing private key json",
}
waitFlag = cli.BoolFlag{
Name: "wait",
Usage: "whether to wait for termination",
}
inputFlag = cli.StringFlag{
Name: "input",
Value: "",
Usage: "input to the program (e.g. stdin)",
}
)
func main() {
@ -48,9 +57,9 @@ func main() {
Name: "run",
Usage: "run process",
Action: cliRunProcess,
Flags: []cli.Flag{
//remotesFlag,
//privKeyFlag,
Flags: []cli.Flag{
waitFlag,
inputFlag,
},
},
cli.Command{
@ -81,7 +90,8 @@ func ParseFlags(c *cli.Context) (remotes []string, privKey acm.PrivKey) {
privkeyFile := c.String("privkey-file")
privkeyJSONBytes, err := ioutil.ReadFile(privkeyFile)
if err != nil {
Exit(Fmt("Failed to read privkey from file %v. %v", privkeyFile, err))
fmt.Printf("Failed to read privkey from file %v. %v", privkeyFile, err)
return remotes, nil
}
binary.ReadJSON(&privKey, privkeyJSONBytes, &err)
if err != nil {
@ -91,14 +101,20 @@ func ParseFlags(c *cli.Context) (remotes []string, privKey acm.PrivKey) {
}
func cliRunProcess(c *cli.Context) {
/*
args := c.Args()
if len(args) == 0 {
log.Fatal("Must specify application name")
}
app := args[0]
*/
command := btypes.CommandRunProcess{}
args := c.Args()
if len(args) < 2 {
Exit("Must specify <label> <execPath> <args...>")
}
label := args[0]
execPath := args[1]
args = args[2:]
command := btypes.CommandRunProcess{
Wait: c.Bool("wait"),
Label: label,
ExecPath: execPath,
Args: args,
Input: c.String("input"),
}
for _, remote := range remotes {
response, err := RunProcess(privKey, remote, command)
if err != nil {
@ -110,14 +126,14 @@ func cliRunProcess(c *cli.Context) {
}
func cliStopProcess(c *cli.Context) {
/*
args := c.Args()
if len(args) == 0 {
log.Fatal("Must specify application name")
}
app := args[0]
*/
command := btypes.CommandStopProcess{}
args := c.Args()
if len(args) == 0 {
Exit("Must specify label to stop")
}
label := args[0]
command := btypes.CommandStopProcess{
Label: label,
}
for _, remote := range remotes {
response, err := StopProcess(privKey, remote, command)
if err != nil {
@ -142,7 +158,12 @@ func cliListProcesses(c *cli.Context) {
if err != nil {
fmt.Printf("%v failure. %v\n", remote, err)
} else {
fmt.Printf("%v success: %v\n", remote, response)
fmt.Printf("%v processes:\n", remote)
for _, proc := range response.Processes {
fmt.Printf(" \"%v\" => `%v` (%v) start:%v end:%v output:%v\n",
proc.Label, proc.ExecPath, proc.Pid,
proc.StartTime, proc.EndTime, proc.OutputPath)
}
}
}
}

+ 11
- 4
process/process.go View File

@ -22,7 +22,9 @@ func makeFile(prefix string) (string, *os.File) {
type Process struct {
Label string
ExecPath string
Pid int
StartTime time.Time
EndTime time.Time
OutputPath string
Cmd *exec.Cmd `json:"-"`
ExitState *os.ProcessState `json:"-"`
@ -36,7 +38,7 @@ const (
// execPath: command name
// args: args to command. (should not include name)
func Create(mode int, label string, execPath string, args []string, input string) *Process {
func Create(mode int, label string, execPath string, args []string, input string) (*Process, error) {
outPath, outFile := makeFile(label)
cmd := exec.Command(execPath, args...)
switch mode {
@ -53,20 +55,25 @@ func Create(mode int, label string, execPath string, args []string, input string
cmd.Stdin = bytes.NewReader([]byte(input))
}
if err := cmd.Start(); err != nil {
fmt.Printf("Failed to run command. %v\n", err)
return nil
return nil, err
} else {
fmt.Printf("Success!")
}
return &Process{
proc := &Process{
Label: label,
ExecPath: execPath,
Pid: cmd.Process.Pid,
StartTime: time.Now(),
OutputPath: outPath,
Cmd: cmd,
ExitState: nil,
OutputFile: outFile,
}
go func() {
Wait(proc)
proc.EndTime = time.Now() // TODO make this goroutine-safe
}()
return proc, nil
}
func Wait(proc *Process) error {


+ 3
- 0
rpc/client.go View File

@ -8,6 +8,7 @@ import (
"net/http"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
func Call(remote string, method string, params []interface{}, dest interface{}) (interface{}, error) {
@ -30,6 +31,8 @@ func Call(remote string, method string, params []interface{}, dest interface{})
return dest, err
}
log.Debug(Fmt("RPC response: %v", string(responseBytes)))
// Parse response into JSONResponse
response := RPCResponse{}
err = json.Unmarshal(responseBytes, &response)


Loading…
Cancel
Save