2022-10-22 17:38:39 +08:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/model"
|
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/service/runner"
|
2024-01-06 16:06:16 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/file"
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
2022-10-22 17:38:39 +08:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"path/filepath"
|
2022-10-23 17:29:35 +08:00
|
|
|
"time"
|
2022-10-22 17:38:39 +08:00
|
|
|
)
|
|
|
|
|
2024-01-29 21:15:39 +08:00
|
|
|
var (
|
|
|
|
statusSystemError = runner.JudgeStatus{
|
|
|
|
Message: "System Error",
|
|
|
|
CompileMessage: "",
|
|
|
|
Tasks: []runner.TaskStatus{{Verdict: runner.VerdictSystemError, Message: "System Error"}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-22 17:38:39 +08:00
|
|
|
func (h *handler) Judge(_ context.Context, t *asynq.Task) error {
|
|
|
|
var p model.SubmitJudgePayload
|
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
|
|
|
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
user := utils.RandomString(16)
|
2024-01-27 17:37:27 +08:00
|
|
|
h.log.Debug("judge", zap.Any("payload", p), zap.String("user", user))
|
2024-01-29 21:15:39 +08:00
|
|
|
meta := runner.JudgeMeta{
|
|
|
|
Run: runner.JudgeMetaRun{
|
|
|
|
Version: p.ProblemVersionID,
|
|
|
|
User: user,
|
|
|
|
Lang: p.Submission.Language,
|
|
|
|
},
|
2024-01-27 23:07:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
status, point, ctx := func() (e.Status, int32, *runner.JudgeStatus) {
|
2022-10-23 17:29:35 +08:00
|
|
|
// 1. write user code
|
|
|
|
userCode := filepath.Join(runner.UserDir, user, fmt.Sprintf("%s.%s", user, p.Submission.Language))
|
2024-01-06 17:31:00 +08:00
|
|
|
if !file.Touch(userCode) {
|
2024-01-29 21:15:39 +08:00
|
|
|
return e.InternalError, 0, &statusSystemError
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|
2024-01-06 17:31:00 +08:00
|
|
|
err := file.Write(userCode, []byte(p.Submission.Code))
|
2022-10-23 17:29:35 +08:00
|
|
|
if err != nil {
|
2024-01-29 21:15:39 +08:00
|
|
|
return e.InternalError, 0, &statusSystemError
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. check problem
|
2024-01-27 23:07:14 +08:00
|
|
|
if !h.runnerService.ProblemExists(&meta) {
|
2022-10-23 17:29:35 +08:00
|
|
|
url, status := h.storageService.Get(p.StorageKey, time.Second*60*5)
|
|
|
|
if status != e.Success {
|
2024-01-29 21:15:39 +08:00
|
|
|
return status, 0, &statusSystemError
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|
2022-10-22 17:38:39 +08:00
|
|
|
|
2024-01-27 23:07:14 +08:00
|
|
|
_, status = h.runnerService.NewProblem(&meta, url, false)
|
2022-10-23 17:29:35 +08:00
|
|
|
if status != e.Success {
|
2024-01-29 21:15:39 +08:00
|
|
|
return status, 0, &statusSystemError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. validate
|
|
|
|
if ret, status := h.runnerService.ValidatePath(&meta); status != e.Success {
|
|
|
|
return status, 0, ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. extract config
|
|
|
|
if status := h.runnerService.GetConfig(&meta, true); status != e.Success {
|
|
|
|
return e.RunnerProblemParseFailed, 0, &runner.JudgeStatus{
|
|
|
|
Message: fmt.Sprintf("System Error: Problem Malformed (%d:%s)", status, status.String()),
|
|
|
|
CompileMessage: "",
|
|
|
|
Tasks: []runner.TaskStatus{{Verdict: runner.VerdictSystemError, Message: "System Error"}},
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 21:15:39 +08:00
|
|
|
// 5. compile
|
2024-01-27 23:07:14 +08:00
|
|
|
compileResult, status := h.runnerService.Compile(&meta)
|
2022-10-22 17:38:39 +08:00
|
|
|
if status != e.Success {
|
2024-01-07 00:45:16 +08:00
|
|
|
return status, 0, compileResult
|
2022-10-22 17:38:39 +08:00
|
|
|
}
|
|
|
|
|
2024-01-29 21:15:39 +08:00
|
|
|
// 6. run and judge
|
2024-01-27 23:07:14 +08:00
|
|
|
result, point, status := h.runnerService.RunAndJudge(&meta)
|
2024-01-29 21:16:51 +08:00
|
|
|
result.CompileMessage = compileResult.CompileMessage
|
2024-01-07 00:45:16 +08:00
|
|
|
return status, point, result
|
2022-10-23 17:29:35 +08:00
|
|
|
}()
|
2022-10-22 17:38:39 +08:00
|
|
|
|
2024-01-07 00:45:16 +08:00
|
|
|
if status == e.InternalError {
|
|
|
|
// notice asynq to retry
|
2024-01-27 23:07:14 +08:00
|
|
|
return fmt.Errorf("internal error, ctx: %v", *ctx)
|
2024-01-07 00:45:16 +08:00
|
|
|
}
|
|
|
|
|
2022-10-23 17:29:35 +08:00
|
|
|
h.taskService.SubmitUpdate(&model.SubmitUpdatePayload{
|
|
|
|
Status: status,
|
|
|
|
SubmissionID: p.Submission.ID,
|
|
|
|
ProblemVersionID: p.ProblemVersionID,
|
2024-01-06 20:34:16 +08:00
|
|
|
UserDir: user,
|
2022-10-23 17:29:35 +08:00
|
|
|
Point: point,
|
2024-01-27 23:07:14 +08:00
|
|
|
}, *ctx)
|
2022-10-22 17:38:39 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|