From f8d26e0c571ccd2adaf921a3a9ea331756fb065b Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 19 May 2019 01:03:33 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=9A=E8=BF=87=20docker=20rm=20?= =?UTF-8?q?=E5=9B=9E=E6=94=B6=E5=AE=B9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- output/run.go | 23 ++++++++++++++++------- playground/run.go | 8 ++++---- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/output/run.go b/output/run.go index 7433665..fa2b797 100644 --- a/output/run.go +++ b/output/run.go @@ -21,6 +21,7 @@ import ( "net/http" "os/exec" "path/filepath" + "strconv" "strings" "time" @@ -49,10 +50,12 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { filePath := args["executable"].(string) + randInt := rand.Int() + rid := strconv.Itoa(randInt) var cmd *exec.Cmd if conf.Docker { fileName := filepath.Base(filePath) - cmd = exec.Command("docker", "run", "--rm", "--cpus", "0.1", "-v", filePath+":/"+fileName, conf.DockerImageGo, "/"+fileName) + cmd = exec.Command("docker", "run", "--rm", "--cpus", "0.1", "--name", rid, "-v", filePath+":/"+fileName, conf.DockerImageGo, "/"+fileName) } else { cmd = exec.Command(filePath) curDir := filepath.Dir(filePath) @@ -107,11 +110,10 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { } } - rid := rand.Int() - go func(runningId int) { + go func() { defer util.Recover() - logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.UserId, sid, runningId, filePath) + logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.UserId, sid, rid, filePath) go func() { defer util.Recover() @@ -152,13 +154,20 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { wsChannel.Refresh() } } - }(rid) + }() after := time.After(5 * time.Second) channelRet["cmd"] = "run-done" select { case <-after: - cmd.Process.Kill() + if conf.Docker { + killCmd := exec.Command("docker", "rm", "-f", rid) + if err := killCmd.Run(); nil != err { + logger.Errorf("executes [docker rm -f " + rid + "] failed [" + err.Error() + "], this will cause resource leaking") + } + } else { + cmd.Process.Kill() + } channelRet["output"] = "run program timeout in 5s\n" case <-done: @@ -166,7 +175,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { } Processes.Remove(wSession, cmd.Process) - logger.Debugf("User [%s, %s] done running [id=%d, file=%s]", wSession.UserId, sid, rid, filePath) + logger.Debugf("User [%s, %s] done running [id=%s, file=%s]", wSession.UserId, sid, rid, filePath) if nil != wsChannel { wsChannel.WriteJSON(&channelRet) diff --git a/playground/run.go b/playground/run.go index eb38c6f..8980015 100644 --- a/playground/run.go +++ b/playground/run.go @@ -112,10 +112,10 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { } } - go func(runningId string) { + go func() { defer util.Recover() - logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.UserId, sid, runningId, filePath) + logger.Debugf("User [%s, %s] is running [id=%s, file=%s]", wSession.UserId, sid, rid, filePath) go func() { defer util.Recover() @@ -156,7 +156,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { wsChannel.Refresh() } } - }(rid) + }() after := time.After(5 * time.Second) channelRet["cmd"] = "run-done" @@ -177,7 +177,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { } output.Processes.Remove(wSession, cmd.Process) - logger.Debugf("User [%s, %s] done running [id=%d, file=%s]", wSession.UserId, sid, rid, filePath) + logger.Debugf("User [%s, %s] done running [id=%s, file=%s]", wSession.UserId, sid, rid, filePath) if nil != wsChannel { wsChannel.WriteJSON(&channelRet)