-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission_bridge.go
71 lines (61 loc) · 2.07 KB
/
submission_bridge.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
package judger
import (
"fmt"
"github.com/Rabbit-OJ/Rabbit-OJ-Judger/config"
"github.com/Rabbit-OJ/Rabbit-OJ-Judger/logger"
JudgerModels "github.com/Rabbit-OJ/Rabbit-OJ-Judger/models"
"github.com/Rabbit-OJ/Rabbit-OJ-Judger/mq"
"github.com/Rabbit-OJ/Rabbit-OJ-Judger/protobuf"
"time"
"github.com/golang/protobuf/proto"
)
func JudgeRequestBridge(body []byte) {
judgeRequest := &protobuf.JudgeRequest{}
if err := proto.Unmarshal(body, judgeRequest); err != nil {
logger.Println(err)
return
}
if config.Global.Extensions.Expire.Enabled &&
judgeRequest.Time-time.Now().Unix() > config.Global.Extensions.CheckJudge.Interval*int64(time.Minute) {
logger.Printf("[Bridge] Received expired judge %d , will ignore this\n", judgeRequest.Sid)
return
}
status, report, err := Scheduler(judgeRequest)
sid := judgeRequest.Sid
if status == "Internal Error" {
logger.Printf("(%d) [Bridge] Requeued due to %+v \n", sid, err)
Requeue(config.JudgeRequestTopicName, body)
} else if status != "OK" {
logger.Printf("(%d) [Bridge] Calling back results \n", judgeRequest.Sid)
CallbackAllError(status, sid, judgeRequest.IsContest, len(report))
} else if status == "OK" {
logger.Printf("(%d) [Bridge] Calling back results \n", judgeRequest.Sid)
CallbackSuccess(sid, judgeRequest.IsContest, report)
}
}
func JudgeResponseBridge(body []byte) {
judgeResult := &protobuf.JudgeResponse{}
if err := proto.Unmarshal(body, judgeResult); err != nil {
logger.Println(err)
return
}
judgeCaseResult := make([]*JudgerModels.JudgeResult, len(judgeResult.Result))
for i, item := range judgeResult.Result {
judgeCaseResult[i] = &JudgerModels.JudgeResult{
Status: item.Status,
TimeUsed: item.TimeUsed,
SpaceUsed: item.SpaceUsed,
}
}
for _, cb := range OnJudgeResponse {
cb(judgeResult.Sid, judgeResult.IsContest, judgeCaseResult)
}
}
func Requeue(topic string, body []byte) {
mq.PublishMessageAsync(topic, []byte(fmt.Sprintf("%d", time.Now().UnixNano())), body)
}
func JudgeResultHandler() {
for delivery := range mq.JudgeResponseDeliveryChan {
go JudgeResponseBridge(delivery)
}
}