-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker.go
82 lines (73 loc) · 1.76 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package controller
import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/Sirupsen/logrus"
"github.com/VOID001/D-judge/config"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
)
type runinfo struct {
usedmem uint64
usedtime int64
outputexceed bool
timeexceed bool
memexceed bool
}
type Worker struct {
JudgeInfo config.JudgeInfo
WorkDir string
DockerImage string
RunUser string
CPUID int
MaxRetryTime int
containerID string
codeFileName string
}
const (
FilePerm = 0644
DirPerm = 0755
SandboxRoot = "/sandbox"
)
func (w *Worker) cleanup(ctx context.Context) (err error) {
log.Debugf("doing cleanup for containerID %s", w.containerID)
cli, er := client.NewClient(config.GlobalConfig.DockerServer, config.GlobalConfig.DockerVersion, nil, nil)
if er != nil {
err = errors.Wrap(er, "worker cleanup error")
return err
}
err = cli.ContainerStop(ctx, w.containerID, nil)
if err != nil {
err = errors.Wrap(err, "worker cleanup error")
return err
}
err = cli.ContainerRemove(ctx, w.containerID, types.ContainerRemoveOptions{})
if err != nil {
err = errors.Wrap(err, "worker cleanup error")
return err
}
return
}
func (w *Worker) readExitCode(ctx context.Context) (code int, err error) {
// Read the file exitcode and return
path := filepath.Join(w.WorkDir, "exitcode")
data, er := ioutil.ReadFile(path)
if er != nil {
err = errors.Wrap(er, "read exit code from file error")
return
}
str := fmt.Sprintf("%s", data)
str = strings.TrimSuffix(str, "\n")
str = strings.TrimPrefix(str, "\n")
code, err = strconv.Atoi(str)
if err != nil {
err = errors.Wrap(err, "read exit code from file error")
return
}
return
}