From ab4feaba816f38f7a5e3c099bbc9524e9f6e6ab9 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 18 Jan 2020 15:40:52 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20Docker=20=E7=8E=AF=E5=A2=83=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=BB=B6=E6=97=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session/processes.go | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/session/processes.go b/session/processes.go index a963ba8..6c94392 100644 --- a/session/processes.go +++ b/session/processes.go @@ -15,7 +15,7 @@ package session import ( - "bufio" + "bytes" "encoding/json" "math/rand" "net/http" @@ -68,27 +68,17 @@ func RunHandler(w http.ResponseWriter, r *http.Request, channel map[string]*util var cmd *exec.Cmd if conf.Docker { fileName := filepath.Base(filePath) - cmd = exec.Command("docker", "run", "-t", "--rm", "--cpus", "0.05", "--name", rid, "-v", filePath+":/"+fileName, conf.DockerImageGo, "/"+fileName) + cmd = exec.Command("docker", "run", "--rm", "--cpus", "0.05", "--name", rid, "-v", filePath+":/"+fileName, conf.DockerImageGo, "/"+fileName) } else { cmd = exec.Command(filePath) curDir := filepath.Dir(filePath) cmd.Dir = curDir } - stdout, err := cmd.StdoutPipe() - if nil != err { - logger.Error(err) - result.Code = -1 - } - - stderr, err := cmd.StderrPipe() - if nil != err { - logger.Error(err) - result.Code = -1 - } - - outReader := bufio.NewReader(stdout) - errReader := bufio.NewReader(stderr) + outBuf := &bytes.Buffer{} + errBuf := &bytes.Buffer{} + cmd.Stdout = outBuf + cmd.Stderr = errBuf if err := cmd.Start(); nil != err { logger.Error(err) @@ -110,6 +100,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request, channel map[string]*util channelRet["pid"] = cmd.Process.Pid Processes.Add(wSession, cmd.Process) + shouldExitBuf := false // push once for front-end to get the 'run' state and pid if nil != wsChannel { @@ -128,11 +119,21 @@ func RunHandler(w http.ResponseWriter, r *http.Request, channel map[string]*util go func() { defer gulu.Panic.Recover(nil) for { - r, _, err := outReader.ReadRune() - if nil != err { + if shouldExitBuf { break } + if 1 > outBuf.Len() { + time.Sleep(7 * time.Millisecond) + continue + } + + r, _, err := outBuf.ReadRune() + if nil != err { + time.Sleep(7 * time.Millisecond) + continue + } + oneRuneStr := string(r) oneRuneStr = strings.Replace(oneRuneStr, "<", "<", -1) oneRuneStr = strings.Replace(oneRuneStr, ">", ">", -1) @@ -147,11 +148,21 @@ func RunHandler(w http.ResponseWriter, r *http.Request, channel map[string]*util }() for { - r, _, err := errReader.ReadRune() - if nil != err { + if shouldExitBuf { break } + if 1 > errBuf.Len() { + time.Sleep(7 * time.Millisecond) + continue + } + + r, _, err := errBuf.ReadRune() + if nil != err { + time.Sleep(7 * time.Millisecond) + continue + } + oneRuneStr := string(r) oneRuneStr = strings.Replace(oneRuneStr, "<", "<", -1) oneRuneStr = strings.Replace(oneRuneStr, ">", ">", -1) @@ -184,6 +195,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request, channel map[string]*util channelRet["output"] = "\nrun program complete\n" } + shouldExitBuf = true Processes.Remove(wSession, cmd.Process) logger.Debugf("User [%s, %s] done running [id=%s, file=%s, kill=%v]", wSession.UserId, sid, rid, filePath, kill)