示例#1
0
func StartProcess(wait bool, label string, execPath string, args []string, input string) (*ResponseStartProcess, error) {
	// First, see if there already is a process labeled 'label'
	existing := barak_.GetProcess(label)
	if existing != nil && existing.EndTime.IsZero() {
		return nil, fmt.Errorf("Process already exists: %v", label)
	}

	// Otherwise, create one.
	err := EnsureDir(barak_.RootDir() + "/outputs")
	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"))
	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
	}
	barak_.AddProcess(label, proc)

	if wait {
		<-proc.WaitCh

		// 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{
				Success: true,
				Output:  output,
			}, nil
		} else {
			return &ResponseStartProcess{
				Success: proc.ExitState.Success(), // Would be always false?
				Output:  output,
			}, nil
		}
	} else {
		return &ResponseStartProcess{
			Success: true,
			Output:  "",
		}, nil
	}
}
示例#2
0
文件: main.go 项目: jaekwon/GuppyCamp
func RunProcess(wait bool, label string, execPath string, args []string, input string) (*ResponseRunProcess, error) {
	barak.mtx.Lock()

	// First, see if there already is a process labeled 'label'
	existing := barak.processes[label]
	if existing != nil && existing.EndTime.IsZero() {
		barak.mtx.Unlock()
		return nil, fmt.Errorf("Process already exists: %v", label)
	}

	// Otherwise, create one.
	err := EnsureDir(barak.rootDir + "/outputs")
	if err != nil {
		return nil, fmt.Errorf("Failed to create outputs dir: %v", err)
	}
	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)
	if err == nil {
		barak.processes[label] = proc
	}
	barak.mtx.Unlock()
	if err != nil {
		return nil, err
	}

	if wait {
		<-proc.WaitCh
		output := pcm.ReadOutput(proc)
		fmt.Println("Read output", output)
		if proc.ExitState == nil {
			return &ResponseRunProcess{
				Success: true,
				Output:  output,
			}, nil
		} else {
			return &ResponseRunProcess{
				Success: proc.ExitState.Success(), // Would be always false?
				Output:  output,
			}, nil
		}
	} else {
		return &ResponseRunProcess{
			Success: true,
			Output:  "",
		}, nil
	}
}