🎨 通过 docker rm 回收容器

This commit is contained in:
Liang Ding 2019-05-19 01:03:33 +08:00
parent 02a809803e
commit f8d26e0c57
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 20 additions and 11 deletions

View File

@ -21,6 +21,7 @@ import (
"net/http" "net/http"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
@ -49,10 +50,12 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
filePath := args["executable"].(string) filePath := args["executable"].(string)
randInt := rand.Int()
rid := strconv.Itoa(randInt)
var cmd *exec.Cmd var cmd *exec.Cmd
if conf.Docker { if conf.Docker {
fileName := filepath.Base(filePath) 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 { } else {
cmd = exec.Command(filePath) cmd = exec.Command(filePath)
curDir := filepath.Dir(filePath) curDir := filepath.Dir(filePath)
@ -107,11 +110,10 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
rid := rand.Int() go func() {
go func(runningId int) {
defer util.Recover() 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() { go func() {
defer util.Recover() defer util.Recover()
@ -152,13 +154,20 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
wsChannel.Refresh() wsChannel.Refresh()
} }
} }
}(rid) }()
after := time.After(5 * time.Second) after := time.After(5 * time.Second)
channelRet["cmd"] = "run-done" channelRet["cmd"] = "run-done"
select { select {
case <-after: 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"] = "<span class='stderr'>run program timeout in 5s</span>\n" channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
case <-done: case <-done:
@ -166,7 +175,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
Processes.Remove(wSession, cmd.Process) 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 { if nil != wsChannel {
wsChannel.WriteJSON(&channelRet) wsChannel.WriteJSON(&channelRet)

View File

@ -112,10 +112,10 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
go func(runningId string) { go func() {
defer util.Recover() 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() { go func() {
defer util.Recover() defer util.Recover()
@ -156,7 +156,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
wsChannel.Refresh() wsChannel.Refresh()
} }
} }
}(rid) }()
after := time.After(5 * time.Second) after := time.After(5 * time.Second)
channelRet["cmd"] = "run-done" channelRet["cmd"] = "run-done"
@ -177,7 +177,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
} }
output.Processes.Remove(wSession, cmd.Process) 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 { if nil != wsChannel {
wsChannel.WriteJSON(&channelRet) wsChannel.WriteJSON(&channelRet)