Skip to content

Commit

Permalink
docker test backup
Browse files Browse the repository at this point in the history
  • Loading branch information
cranemont committed Aug 20, 2022
1 parent fc931c7 commit 77cdd99
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 47 deletions.
3 changes: 2 additions & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const TASK_EXEC = "Execute"
const TASK_EXITED = "Exited"
const PUBLISH_RESULT = "Publish"

const BASE_DIR = "./results"
// const BASE_DIR = "./results"
const BASE_DIR = "/home/coc0a25/go/src/github.com/cranemont/judge-manager/results"
const BASE_FILE_MODE = 0522
4 changes: 2 additions & 2 deletions fileManager/fileManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func NewFileManager() *FileManager {
}

func (f *FileManager) CreateDir(name string) {
os.Mkdir(f.basePath+"/"+name, os.FileMode(constants.BASE_FILE_MODE))
os.Mkdir(constants.BASE_DIR+"/"+name, os.FileMode(constants.BASE_FILE_MODE))
}

func (f *FileManager) RemoveDir(name string) {
os.RemoveAll(f.basePath + "/" + name)
os.RemoveAll(constants.BASE_DIR + "/" + name)
}
40 changes: 37 additions & 3 deletions judge/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package judge

import (
"fmt"
"io/ioutil"
"strings"
"time"

"github.com/cranemont/judge-manager/constants"
"github.com/cranemont/judge-manager/judge/config"
)

type Compiler interface {
Compile(task *Task)
Compile(task *Task) // 얘는 task 몰라도 됨
createSrcFile(srcPath string, code string) error
}

type compiler struct {
Expand All @@ -17,13 +21,43 @@ type compiler struct {
}

func NewCompiler(sandbox Sandbox, option *config.CompileOption) *compiler {
option.Init()
return &compiler{sandbox, option}
}

func (c *compiler) Compile(task *Task) {
fmt.Println("Compile! from Compiler")
// go c.sandbox.Execute() // wait을 해야지, 아니면 sync로 돌리등가
c.sandbox.Execute()

options := c.option.Get(task.language) // 이게 된다고? private 아닌가? GetLanguage 가 필요없어?
srcPath := constants.BASE_DIR + "/" + task.GetDir() + "/" + options.SrcName
exePath := constants.BASE_DIR + "/" + task.GetDir() + "/" + options.ExeName

// task.code로 srcName에 파일 생성
c.createSrcFile(srcPath, task.code)

// option에서 바로 매칭시켜서 sadnbox인자 넘겨주기

args := strings.Replace(options.Args, "{srcPath}", srcPath, 1)
args = strings.Replace(args, "{exePath}", exePath, 1)
argSlice := strings.Split(args, " ")

c.sandbox.Execute(
&SandboxArgs{
ExePath: options.CompilerPath,
MaxCpuTime: options.MaxCpuTime,
MaxRealTime: options.MaxRealTime,
MaxMemory: options.MaxMemory,
Args: argSlice,
})
time.Sleep(time.Second * 3)
// 채널로 결과반환?
}

func (c *compiler) createSrcFile(srcPath string, code string) error {
err := ioutil.WriteFile(srcPath, []byte(code), 0644)
if err != nil {
fmt.Println("파일 생성 실패")
return err
}
return nil
}
12 changes: 0 additions & 12 deletions judge/config/compile.go

This file was deleted.

60 changes: 60 additions & 0 deletions judge/config/compileOption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package config

// 언어별 설정 관리..
// 각 DTO로 변환 과정 수행

type option struct {
SrcName string
ExeName string
MaxCpuTime int
MaxRealTime int
MaxMemory int
CompilerPath string
Args string
}
type CompileOption struct {
// Get(language) -> constants패키지에서 설정값 가져옴
optionMap map[string]*option
}

// enum으로 바꾸기
func (c *CompileOption) Get(lang string) *option {
return c.optionMap[lang]
}

func (c *CompileOption) Init() {
c.optionMap = make(map[string]*option)
// srcPath, exePath는 base dir + task dir
c.optionMap["c"] = &option{
SrcName: "main.c",
ExeName: "main",
MaxCpuTime: 3000,
MaxRealTime: 10000,
MaxMemory: 256 * 1024 * 1024,
CompilerPath: "/usr/bin/gcc",
Args: "-DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c11 {srcPath} -lm -o {exePath}",
}
c.optionMap["cpp"] = &option{
SrcName: "main.cpp",
ExeName: "main",
MaxCpuTime: 10000,
MaxRealTime: 20000,
MaxMemory: 1024 * 1024 * 1024,
CompilerPath: "/usr/bin/g++",
Args: "-DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c++14 {src_path} -lm -o {exe_path}",
}
}

// "compile": {
// "src_name": "main.c",
// "exe_name": "main",
// "max_cpu_time": 3000,
// "max_real_time": 10000,
// "max_memory": 256 * 1024 * 1024,
// "command": "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c11 {src_path} -lm -o {exe_path}",
// },
// "run": {
// "command": "{exe_path}",
// "seccomp_rule": {ProblemIOMode.standard: "c_cpp", ProblemIOMode.file: "c_cpp_file_io"},
// "env": default_env
// }
File renamed without changes.
62 changes: 62 additions & 0 deletions judge/config/sandboxArgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

// "language_config": sub_config["config"],
// "src": code,
// "max_cpu_time": self._change_time(language, self.problem.time_limit),
// "max_memory": self._change_memory(language, self.problem.memory_limit),
// "test_case_id": self.problem.test_case_id,
// "output": False,
// "spj_version": self.problem.spj_version,
// "spj_config": spj_config.get("config"),
// "spj_compile_config": spj_config.get("compile"),
// "spj_src": self.problem.spj_code,
// "io_mode": self.problem.io_mode
// --max_cpu_time=<n> Max CPU Time (ms)
// --max_real_time=<n> Max Real Time (ms)
// --max_memory=<n> Max Memory (byte)
// --memory_limit_check_only=<n> only check memory usage, do not setrlimit (default False)
// --max_stack=<n> Max Stack (byte, default 16M)
// --max_process_number=<n> Max Process Number
// --max_output_size=<n> Max Output Size (byte)
// --exe_path=<str> Exe Path
// --input_path=<str> Input Path
// --output_path=<str> Output Path
// --error_path=<str> Error Path
// --args=<str> Arg
// --env=<str> Env
// --log_path=<str> Log Path
// --seccomp_rule_name=<str> Seccomp Rule Name
// --uid=<n> UID (default 65534)
// --gid=<n>\

// max_cpu_time=self._max_cpu_time,
// max_real_time=self._max_real_time,
// max_memory=self._max_memory,
// max_stack=128 * 1024 * 1024,
// max_output_size=max(test_case_info.get("output_size", 0) * 2, 1024 * 1024 * 16),
// max_process_number=_judger.UNLIMITED,
// exe_path=command[0],
// args=command[1::],
// env=env,
// log_path=JUDGER_RUN_LOG_PATH,
// seccomp_rule_name=seccomp_rule,
// uid=RUN_USER_UID,
// gid=RUN_GROUP_GID,
// memory_limit_check_only=self._run_config.get("memory_limit_check_only", 0),
// kwargs = {"input_path": in_file, "output_path": real_user_output_file, "error_path": real_user_output_file}

// self._run_config = run_config
// self._exe_path = exe_path
// self._max_cpu_time = max_cpu_time
// self._max_memory = max_memory
// self._max_real_time = self._max_cpu_time * 3
// self._test_case_dir = test_case_dir
// self._submission_dir = submission_dir

// self._pool = Pool(processes=psutil.cpu_count())
// self._test_case_info = self._load_test_case_info()

// self._spj_version = spj_version
// self._spj_config = spj_config
// self._output = output
// self._io_mode = io_mode
11 changes: 9 additions & 2 deletions judge/grader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package judge

import "fmt"

type Grader interface {
Grade()
Grade(task *Task, out chan string)
}

type grader struct {
Expand All @@ -11,6 +13,11 @@ func NewGrader() *grader {
return &grader{}
}

func (g *grader) Grade() {
func (g *grader) Grade(task *Task, out chan string) {
// 일단 파일로 읽어서 채점
// sed로 날리기
// https://stackoverflow.com/questions/20521857/remove-white-space-from-the-end-of-line-in-linux

fmt.Println("grading....")
out <- "task " + task.GetDir() + " done!\n"
}
2 changes: 1 addition & 1 deletion judge/judgeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (j *JudgeController) Judge(task *Task) {
// wait

// err 처리
j.judger.Judge(task)
j.judger.RunAndGrade(task)

// eventManager한테 task done 이벤트 전송
fmt.Println("done")
Expand Down
2 changes: 1 addition & 1 deletion judge/judgeEvent/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *handler) OnExec(task *judge.Task) {
func (h *handler) OnExit(task *judge.Task) {
// 파일 삭제, task 결과 업데이트 등 정리작업
h.eventEmitter.Emit(constants.PUBLISH_RESULT, task)
go h.fileManager.RemoveDir(task.GetDir())
// go h.fileManager.RemoveDir(task.GetDir())
}

func (h *handler) RegisterFn() {
Expand Down
30 changes: 21 additions & 9 deletions judge/judger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type Judger interface {
Compile(task *Task)
Judge(task *Task) // run and grade
RunAndGrade(task *Task) // run and grade
}

type judger struct {
Expand All @@ -21,25 +21,37 @@ func NewJudger(compiler Compiler, runner Runner, grader Grader) *judger {

// err 처리
func (j *judger) Compile(task *Task) {
j.compiler.Compile(task)
j.compiler.Compile(task) // 여기서는 인자 정리해서 넘겨주기
}

// func (j *judger) Run(task *Task, out chan<- string)

// err 처리, Run이랑 Grade로 분리
func (j *judger) Judge(task *Task) {
func (j *judger) RunAndGrade(task *Task) {
// run and grade
tcNum := task.GetTestcase().GetTotal()

ch := make(chan string, tcNum)
runCh := make(chan string, tcNum)
for i := 0; i < tcNum; i++ {
go j.runner.Run(task)
go j.runner.Run(task, runCh) // 여기서는 인자 정리해서 넘겨주기
}

gradeCh := make(chan string, tcNum)
for i := 0; i < tcNum; i++ {
result := <-ch
fmt.Printf("%s Done!\n", result)
result := <-runCh
// result에 따라서 grade할지, 다른방식 쓸지 결정
// run 결과를 파일로?
fmt.Printf("%s grader running\n", result)
go j.grader.Grade(task, gradeCh) // 여기서는 인자 정리해서 넘겨주기
// 여기서 이제 grade 고루틴으로 정리
}
close(ch)
// close chan

finalResult := ""
for i := 0; i < tcNum; i++ {
gradeResult := <-gradeCh
finalResult += gradeResult
// task에 결과 반영
}

fmt.Println(finalResult)
}
8 changes: 5 additions & 3 deletions judge/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Runner interface {
Run(task *Task)
Run(task *Task, out chan<- string)
}

type runner struct {
Expand All @@ -19,8 +19,10 @@ func NewRunner(sandbox Sandbox, option *config.RunOption) *runner {
return &runner{sandbox, option}
}

func (r *runner) Run(task *Task) {
func (r *runner) Run(task *Task, out chan<- string) {
fmt.Println("RUN! from runner")
r.sandbox.Execute()
// r.sandbox.Execute()
dir := task.GetDir()
out <- "task " + dir + " running done"
// 채널로 결과반환
}
Loading

0 comments on commit 77cdd99

Please sign in to comment.