Build&Run

This commit is contained in:
Liang Ding 2014-11-06 17:48:03 +08:00
parent 720cb0746b
commit 880fd5a86d
1 changed files with 27 additions and 33 deletions

View File

@ -62,16 +62,12 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&args); err != nil { if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
glog.Error(err) glog.Error(err)
data["succ"] = false data["succ"] = false
return
} }
sid := args["sid"].(string) sid := args["sid"].(string)
wSession := session.WideSessions.Get(sid) wSession := session.WideSessions.Get(sid)
if nil == wSession { if nil == wSession {
data["succ"] = false data["succ"] = false
return
} }
filePath := args["executable"].(string) filePath := args["executable"].(string)
@ -84,16 +80,12 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if nil != err { if nil != err {
glog.Error(err) glog.Error(err)
data["succ"] = false data["succ"] = false
return
} }
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if nil != err { if nil != err {
glog.Error(err) glog.Error(err)
data["succ"] = false data["succ"] = false
return
} }
outReader := bufio.NewReader(stdout) outReader := bufio.NewReader(stdout)
@ -102,16 +94,34 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if err := cmd.Start(); nil != err { if err := cmd.Start(); nil != err {
glog.Error(err) glog.Error(err)
data["succ"] = false data["succ"] = false
}
wsChannel := session.OutputWS[sid]
channelRet := map[string]interface{}{}
if !data["succ"].(bool) {
if nil != wsChannel {
channelRet["cmd"] = "run-done"
channelRet["output"] = ""
err := wsChannel.Conn.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
}
wsChannel.Refresh()
}
return return
} }
channelRet["pid"] = cmd.Process.Pid
// add the process to user's process set // add the process to user's process set
processes.add(wSession, cmd.Process) processes.add(wSession, cmd.Process)
channelRet := map[string]interface{}{}
channelRet["pid"] = cmd.Process.Pid
go func(runningId int) { go func(runningId int) {
defer util.Recover() defer util.Recover()
defer cmd.Wait() defer cmd.Wait()
@ -119,9 +129,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
glog.V(3).Infof("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath) glog.V(3).Infof("Session [%s] is running [id=%d, file=%s]", sid, runningId, filePath)
// push once for front-end to get the 'run' state and pid // push once for front-end to get the 'run' state and pid
if nil != session.OutputWS[sid] { if nil != wsChannel {
wsChannel := session.OutputWS[sid]
channelRet["cmd"] = "run" channelRet["cmd"] = "run"
channelRet["output"] = "" channelRet["output"] = ""
err := wsChannel.Conn.WriteJSON(&channelRet) err := wsChannel.Conn.WriteJSON(&channelRet)
@ -145,9 +153,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
glog.V(3).Infof("Session [%s] 's running [id=%d, file=%s] has done [stdout err]", sid, runningId, filePath) glog.V(3).Infof("Session [%s] 's running [id=%d, file=%s] has done [stdout err]", sid, runningId, filePath)
if nil != session.OutputWS[sid] { if nil != wsChannel {
wsChannel := session.OutputWS[sid]
channelRet["cmd"] = "run-done" channelRet["cmd"] = "run-done"
channelRet["output"] = "<pre>" + string(buf) + "</pre>" channelRet["output"] = "<pre>" + string(buf) + "</pre>"
err := wsChannel.Conn.WriteJSON(&channelRet) err := wsChannel.Conn.WriteJSON(&channelRet)
@ -161,9 +167,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
break break
} else { } else {
if nil != session.OutputWS[sid] { if nil != wsChannel {
wsChannel := session.OutputWS[sid]
channelRet["cmd"] = "run" channelRet["cmd"] = "run"
channelRet["output"] = "<pre>" + string(buf) + "</pre>" channelRet["output"] = "<pre>" + string(buf) + "</pre>"
err := wsChannel.Conn.WriteJSON(&channelRet) err := wsChannel.Conn.WriteJSON(&channelRet)
@ -268,27 +272,17 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
if util.OS.IsWindows() { if util.OS.IsWindows() {
suffix = ".exe" suffix = ".exe"
} }
executable := "main" + suffix
argv := []string{"build", "-o", executable}
cmd := exec.Command("go", argv...) cmd := exec.Command("go", "build")
cmd.Dir = curDir cmd.Dir = curDir
setCmdEnv(cmd, username) setCmdEnv(cmd, username)
glog.V(5).Infof("go build -o %s", executable) executable := filepath.Base(curDir) + suffix
glog.V(5).Infof("go build for [%s]", executable)
executable = filepath.Join(curDir, executable) executable = filepath.Join(curDir, executable)
// remove executable file before building
err = os.RemoveAll(executable)
if nil != err {
glog.Info(err)
data["succ"] = false
return
}
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if nil != err { if nil != err {
glog.Error(err) glog.Error(err)