diff options
Diffstat (limited to 'backend/taskqueue/processor.go')
| -rw-r--r-- | backend/taskqueue/processor.go | 148 |
1 files changed, 36 insertions, 112 deletions
diff --git a/backend/taskqueue/processor.go b/backend/taskqueue/processor.go index d771e61..ba35a1b 100644 --- a/backend/taskqueue/processor.go +++ b/backend/taskqueue/processor.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "net/http" - "strings" "github.com/nsfisis/iosdc-japan-2024-albatross/backend/db" ) @@ -46,6 +45,7 @@ func (p *processor) doProcessTaskCompileSwiftToWasm( ctx context.Context, payload *TaskPayloadCompileSwiftToWasm, ) (*TaskResultCompileSwiftToWasm, error) { + _ = ctx type swiftcRequestData struct { MaxDuration int `json:"max_duration_ms"` Code string `json:"code"` @@ -71,27 +71,9 @@ func (p *processor) doProcessTaskCompileSwiftToWasm( if err := json.NewDecoder(res.Body).Decode(&resData); err != nil { return nil, fmt.Errorf("json.Decode failed: %v", err) } - if resData.Status != "success" { - err := p.q.CreateSubmissionResult(ctx, db.CreateSubmissionResultParams{ - SubmissionID: int32(payload.SubmissionID), - Status: "compile_error", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }) - if err != nil { - return nil, fmt.Errorf("CreateTestcaseResult failed: %v", err) - } - return &TaskResultCompileSwiftToWasm{ - TaskPayload: payload, - Status: "compile_error", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }, nil - } - return &TaskResultCompileSwiftToWasm{ TaskPayload: payload, - Status: "success", + Status: resData.Status, Stdout: resData.Stdout, Stderr: resData.Stderr, }, nil @@ -101,6 +83,7 @@ func (p *processor) doProcessTaskCompileWasmToNativeExecutable( ctx context.Context, payload *TaskPayloadCompileWasmToNativeExecutable, ) (*TaskResultCompileWasmToNativeExecutable, error) { + _ = ctx type wasmcRequestData struct { MaxDuration int `json:"max_duration_ms"` Code string `json:"code"` @@ -126,27 +109,9 @@ func (p *processor) doProcessTaskCompileWasmToNativeExecutable( if err := json.NewDecoder(res.Body).Decode(&resData); err != nil { return nil, fmt.Errorf("json.Decode failed: %v", err) } - if resData.Status != "success" { - err := p.q.CreateSubmissionResult(ctx, db.CreateSubmissionResultParams{ - SubmissionID: int32(payload.SubmissionID), - Status: "compile_error", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }) - if err != nil { - return nil, fmt.Errorf("CreateTestcaseResult failed: %v", err) - } - return &TaskResultCompileWasmToNativeExecutable{ - TaskPayload: payload, - Status: "compile_error", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }, nil - } - return &TaskResultCompileWasmToNativeExecutable{ TaskPayload: payload, - Status: "success", + Status: resData.Status, Stdout: resData.Stdout, Stderr: resData.Stderr, }, nil @@ -156,75 +121,40 @@ func (p *processor) doProcessTaskRunTestcase( ctx context.Context, payload *TaskPayloadRunTestcase, ) (*TaskResultRunTestcase, error) { - testcases, err := p.q.ListTestcasesByGameID(ctx, int32(payload.GameID())) + type testrunRequestData struct { + MaxDuration int `json:"max_duration_ms"` + Code string `json:"code"` + Stdin string `json:"stdin"` + } + type testrunResponseData struct { + Status string `json:"status"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` + } + reqData := testrunRequestData{ + MaxDuration: 5000, + Code: payload.Code(), + Stdin: payload.Stdin, + } + reqJson, err := json.Marshal(reqData) if err != nil { - return nil, fmt.Errorf("ListTestcasesByGameID failed: %v", err) + return nil, fmt.Errorf("json.Marshal failed: %v", err) } - - for _, testcase := range testcases { - type testrunRequestData struct { - MaxDuration int `json:"max_duration_ms"` - Code string `json:"code"` - Stdin string `json:"stdin"` - } - type testrunResponseData struct { - Status string `json:"status"` - Stdout string `json:"stdout"` - Stderr string `json:"stderr"` - } - reqData := testrunRequestData{ - MaxDuration: 5000, - Code: payload.Code(), - Stdin: testcase.Stdin, - } - reqJson, err := json.Marshal(reqData) - if err != nil { - return nil, fmt.Errorf("json.Marshal failed: %v", err) - } - res, err := http.Post("http://worker:80/api/testrun", "application/json", bytes.NewBuffer(reqJson)) - if err != nil { - return nil, fmt.Errorf("http.Post failed: %v", err) - } - resData := testrunResponseData{} - if err := json.NewDecoder(res.Body).Decode(&resData); err != nil { - return nil, fmt.Errorf("json.Decode failed: %v", err) - } - if resData.Status != "success" { - err := p.q.CreateTestcaseResult(ctx, db.CreateTestcaseResultParams{ - SubmissionID: int32(payload.SubmissionID), - TestcaseID: testcase.TestcaseID, - Status: resData.Status, - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }) - if err != nil { - return nil, fmt.Errorf("CreateTestcaseResult failed: %v", err) - } - return &TaskResultRunTestcase{ - TaskPayload: payload, - Status: resData.Status, - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }, nil - } - if !isTestcaseResultCorrect(testcase.Stdout, resData.Stdout) { - err := p.q.CreateTestcaseResult(ctx, db.CreateTestcaseResultParams{ - SubmissionID: int32(payload.SubmissionID), - TestcaseID: testcase.TestcaseID, - Status: "wrong_answer", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }) - if err != nil { - return nil, fmt.Errorf("CreateTestcaseResult failed: %v", err) - } - return &TaskResultRunTestcase{ - TaskPayload: payload, - Status: "wrong_answer", - Stdout: resData.Stdout, - Stderr: resData.Stderr, - }, nil - } + res, err := http.Post("http://worker:80/api/testrun", "application/json", bytes.NewBuffer(reqJson)) + if err != nil { + return nil, fmt.Errorf("http.Post failed: %v", err) + } + resData := testrunResponseData{} + if err := json.NewDecoder(res.Body).Decode(&resData); err != nil { + return nil, fmt.Errorf("json.Decode failed: %v", err) + } + if resData.Status != "success" { + return &TaskResultRunTestcase{ + TaskPayload: payload, + Status: resData.Status, + Stdout: resData.Stdout, + Stderr: resData.Stderr, + }, nil } return &TaskResultRunTestcase{ @@ -232,9 +162,3 @@ func (p *processor) doProcessTaskRunTestcase( Status: "success", }, nil } - -func isTestcaseResultCorrect(expectedStdout, actualStdout string) bool { - expectedStdout = strings.TrimSpace(expectedStdout) - actualStdout = strings.TrimSpace(actualStdout) - return actualStdout == expectedStdout -} |
