Fix #202
This commit is contained in:
		
							parent
							
								
									fef4fffce0
								
							
						
					
					
						commit
						18d5d1c09b
					
				| 
						 | 
				
			
			@ -22,12 +22,23 @@ import (
 | 
			
		|||
	"os/exec"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/b3log/wide/conf"
 | 
			
		||||
	"github.com/b3log/wide/session"
 | 
			
		||||
	"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.
 | 
			
		||||
func RunHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	data := map[string]interface{}{"succ": true}
 | 
			
		||||
| 
						 | 
				
			
			@ -122,18 +133,21 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		|||
		}
 | 
			
		||||
 | 
			
		||||
		go func() {
 | 
			
		||||
			for {
 | 
			
		||||
				r, _, err := outReader.ReadRune()
 | 
			
		||||
			buf := outputBuf{}
 | 
			
		||||
 | 
			
		||||
				if nil == session.OutputWS[sid] {
 | 
			
		||||
			for {
 | 
			
		||||
				wsChannel := session.OutputWS[sid]
 | 
			
		||||
				if nil == wsChannel {
 | 
			
		||||
					break
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				wsChannel := session.OutputWS[sid]
 | 
			
		||||
				r, _, err := outReader.ReadRune()
 | 
			
		||||
 | 
			
		||||
				buf := string(r)
 | 
			
		||||
				buf = strings.Replace(buf, "<", "<", -1)
 | 
			
		||||
				buf = strings.Replace(buf, ">", ">", -1)
 | 
			
		||||
				oneRuneStr := string(r)
 | 
			
		||||
				oneRuneStr = strings.Replace(oneRuneStr, "<", "<", -1)
 | 
			
		||||
				oneRuneStr = strings.Replace(oneRuneStr, ">", ">", -1)
 | 
			
		||||
 | 
			
		||||
				buf.content += oneRuneStr
 | 
			
		||||
 | 
			
		||||
				if nil != err {
 | 
			
		||||
					// 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)
 | 
			
		||||
 | 
			
		||||
					if nil != wsChannel {
 | 
			
		||||
					channelRet["cmd"] = "run-done"
 | 
			
		||||
						channelRet["output"] = buf
 | 
			
		||||
					channelRet["output"] = buf.content
 | 
			
		||||
					err := wsChannel.WriteJSON(&channelRet)
 | 
			
		||||
					if nil != err {
 | 
			
		||||
						logger.Error(err)
 | 
			
		||||
| 
						 | 
				
			
			@ -151,14 +164,23 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		|||
					}
 | 
			
		||||
 | 
			
		||||
					wsChannel.Refresh()
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
					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["output"] = buf
 | 
			
		||||
						err := wsChannel.WriteJSON(&channelRet)
 | 
			
		||||
					channelRet["output"] = buf.content
 | 
			
		||||
 | 
			
		||||
					buf = outputBuf{} // a new buffer
 | 
			
		||||
 | 
			
		||||
					err = wsChannel.WriteJSON(&channelRet)
 | 
			
		||||
					if nil != err {
 | 
			
		||||
						logger.Error(err)
 | 
			
		||||
						break
 | 
			
		||||
| 
						 | 
				
			
			@ -167,18 +189,16 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		|||
					wsChannel.Refresh()
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
 | 
			
		||||
		for {
 | 
			
		||||
			r, _, err := errReader.ReadRune()
 | 
			
		||||
 | 
			
		||||
			if nil != err || nil == session.OutputWS[sid] {
 | 
			
		||||
			wsChannel := session.OutputWS[sid]
 | 
			
		||||
			if nil != err || nil == wsChannel {
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			wsChannel := session.OutputWS[sid]
 | 
			
		||||
 | 
			
		||||
			buf := string(r)
 | 
			
		||||
			buf = strings.Replace(buf, "<", "<", -1)
 | 
			
		||||
			buf = strings.Replace(buf, ">", ">", -1)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -418,6 +418,8 @@ var wide = {
 | 
			
		|||
 | 
			
		||||
                    break;
 | 
			
		||||
                case 'run-done':
 | 
			
		||||
                    bottomGroup.fillOutput($('.bottom-window-group .output > div').html().replace(/<\/pre>$/g, data.output + '</pre>'));
 | 
			
		||||
                    
 | 
			
		||||
                    wide.curProcessId = undefined;
 | 
			
		||||
                    $("#buildRun").removeClass("ico-stop")
 | 
			
		||||
                            .addClass("ico-buildrun").attr("title", config.label.build_n_run);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue