From 02a809803e44ac9c80fa8737a44a9e296695b748 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 19 May 2019 00:59:10 +0800 Subject: [PATCH] =?UTF-8?q?:whale:=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 --- playground/run.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/playground/run.go b/playground/run.go index c0d8863..eb38c6f 100644 --- a/playground/run.go +++ b/playground/run.go @@ -21,6 +21,7 @@ import ( "net/http" "os/exec" "path/filepath" + "strconv" "strings" "time" @@ -50,10 +51,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) @@ -109,8 +112,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { } } - rid := rand.Int() - go func(runningId int) { + go func(runningId string) { defer util.Recover() logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.UserId, sid, runningId, filePath) @@ -160,7 +162,14 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { 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"] = "\nrun program timeout in 5s\n" case <-done: