-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (70 loc) · 1.98 KB
/
main.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
83
package main
import (
"log"
"os"
"sync"
"github.com/programming-in-th/grader/api"
"github.com/programming-in-th/grader/conf"
"github.com/programming-in-th/grader/grader"
"github.com/programming-in-th/grader/util"
)
func initGrader(config conf.Config) {
// Create base tmp path for user binaries and outputs
err := util.CreateDirIfNotExist(grader.BASE_TMP_PATH)
if err != nil {
log.Fatal("Error creating working tmp folder")
}
// Create base tmp path for source files (all submissions)
err = util.CreateDirIfNotExist(grader.BASE_SRC_PATH)
if err != nil {
log.Fatalln("Error initializing API: cannot create base src path")
}
gradingJobDoneChannel := make(chan bool)
gradingJobChannel := grader.NewGradingJobQueue(2, gradingJobDoneChannel, config)
// Init handlers
requestDoneChannel := make(chan bool)
requestChannel := newSubmissionJobQueue(4, requestDoneChannel, gradingJobChannel, config)
api.InitAPI(requestChannel, config)
requestDoneChannel <- true
gradingJobDoneChannel <- true
close(gradingJobDoneChannel)
}
func newSubmissionJobQueue(maxWorkers int, done chan bool, gradingJobChannel chan grader.GradingJob, config conf.Config) chan api.GradingRequest {
ch := make(chan api.GradingRequest)
var wg sync.WaitGroup
go func() {
wg.Wait()
close(ch)
}()
wg.Add(maxWorkers)
for i := 0; i < maxWorkers; i++ {
go func() {
for {
select {
case request := <-ch:
err := grader.GradeSubmission(request.SubmissionID, request.TaskID, request.TargLang, request.Code, gradingJobChannel, request.SyncUpdateChannel, config)
if err != nil {
// TODO: do something with the error
log.Println(err)
}
case <-done:
wg.Done()
return
}
}
}()
}
return ch
}
func main() {
err := os.RemoveAll("/var/local/lib/isolate")
if err != nil {
log.Fatal("Failed to rm /var/local/lib/isolate")
}
if len(os.Args) < 2 {
log.Fatal("Base path not provided")
}
basePath := os.Args[1]
config := conf.InitConfig(basePath)
initGrader(config)
}