This commit is contained in:
Liang Ding 2015-01-14 11:09:10 +08:00
parent fef4fffce0
commit 18d5d1c09b
2 changed files with 52 additions and 30 deletions

View File

@ -22,12 +22,23 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
) )
const (
outputBufMax = 128 // 128 string(rune)
outputTimeout = 100 // 100ms
)
type outputBuf struct {
content string
millisecond int64
}
// RunHandler handles request of executing a binary file. // RunHandler handles request of executing a binary file.
func RunHandler(w http.ResponseWriter, r *http.Request) { func RunHandler(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{"succ": true} data := map[string]interface{}{"succ": true}
@ -122,18 +133,21 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
go func() { go func() {
for { buf := outputBuf{}
r, _, err := outReader.ReadRune()
if nil == session.OutputWS[sid] { for {
wsChannel := session.OutputWS[sid]
if nil == wsChannel {
break break
} }
wsChannel := session.OutputWS[sid] r, _, err := outReader.ReadRune()
buf := string(r) oneRuneStr := string(r)
buf = strings.Replace(buf, "<", "&lt;", -1) oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
buf = strings.Replace(buf, ">", "&gt;", -1) oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
buf.content += oneRuneStr
if nil != err { if nil != err {
// remove the exited process from user process set // remove the exited process from user process set
@ -141,9 +155,8 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
logger.Tracef("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ", wSession.Username, sid, runningId, filePath, err) logger.Tracef("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ", wSession.Username, sid, runningId, filePath, err)
if nil != wsChannel {
channelRet["cmd"] = "run-done" channelRet["cmd"] = "run-done"
channelRet["output"] = buf channelRet["output"] = buf.content
err := wsChannel.WriteJSON(&channelRet) err := wsChannel.WriteJSON(&channelRet)
if nil != err { if nil != err {
logger.Error(err) logger.Error(err)
@ -151,14 +164,23 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
wsChannel.Refresh() wsChannel.Refresh()
}
break break
} else { }
if nil != wsChannel {
now := time.Now().UnixNano() / int64(time.Millisecond)
if 0 == buf.millisecond {
buf.millisecond = now
}
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax || oneRuneStr == "\n" {
channelRet["cmd"] = "run" channelRet["cmd"] = "run"
channelRet["output"] = buf channelRet["output"] = buf.content
err := wsChannel.WriteJSON(&channelRet)
buf = outputBuf{} // a new buffer
err = wsChannel.WriteJSON(&channelRet)
if nil != err { if nil != err {
logger.Error(err) logger.Error(err)
break break
@ -167,18 +189,16 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
wsChannel.Refresh() wsChannel.Refresh()
} }
} }
}
}() }()
for { for {
r, _, err := errReader.ReadRune() r, _, err := errReader.ReadRune()
if nil != err || nil == session.OutputWS[sid] { wsChannel := session.OutputWS[sid]
if nil != err || nil == wsChannel {
break break
} }
wsChannel := session.OutputWS[sid]
buf := string(r) buf := string(r)
buf = strings.Replace(buf, "<", "&lt;", -1) buf = strings.Replace(buf, "<", "&lt;", -1)
buf = strings.Replace(buf, ">", "&gt;", -1) buf = strings.Replace(buf, ">", "&gt;", -1)

View File

@ -418,6 +418,8 @@ var wide = {
break; break;
case 'run-done': case 'run-done':
bottomGroup.fillOutput($('.bottom-window-group .output > div').html().replace(/<\/pre>$/g, data.output + '</pre>'));
wide.curProcessId = undefined; wide.curProcessId = undefined;
$("#buildRun").removeClass("ico-stop") $("#buildRun").removeClass("ico-stop")
.addClass("ico-buildrun").attr("title", config.label.build_n_run); .addClass("ico-buildrun").attr("title", config.label.build_n_run);