aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/taskqueue
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-08-08 01:30:09 +0900
committernsfisis <nsfisis@gmail.com>2024-08-08 04:02:37 +0900
commit4eb7e89d6a77a4434bd087fbb86873521d30a8f5 (patch)
treeda13030d94538170a931747b957d2865bed03682 /backend/taskqueue
parent26378c2d2ad1f8cb3f20c7070be3be9a4f0a0ad6 (diff)
downloadphperkaigi-2025-albatross-4eb7e89d6a77a4434bd087fbb86873521d30a8f5.tar.gz
phperkaigi-2025-albatross-4eb7e89d6a77a4434bd087fbb86873521d30a8f5.tar.zst
phperkaigi-2025-albatross-4eb7e89d6a77a4434bd087fbb86873521d30a8f5.zip
feat(backend): implement processTaskResults() partially
Diffstat (limited to 'backend/taskqueue')
-rw-r--r--backend/taskqueue/processor.go148
-rw-r--r--backend/taskqueue/queue.go4
-rw-r--r--backend/taskqueue/tasks.go6
3 files changed, 46 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
-}
diff --git a/backend/taskqueue/queue.go b/backend/taskqueue/queue.go
index b7d7381..515a406 100644
--- a/backend/taskqueue/queue.go
+++ b/backend/taskqueue/queue.go
@@ -83,6 +83,8 @@ func (q *Queue) EnqueueTaskRunTestcase(
code string,
submissionID int,
testcaseID int,
+ stdin string,
+ stdout string,
) error {
task, err := newTaskRunTestcase(
gameID,
@@ -90,6 +92,8 @@ func (q *Queue) EnqueueTaskRunTestcase(
code,
submissionID,
testcaseID,
+ stdin,
+ stdout,
)
if err != nil {
return err
diff --git a/backend/taskqueue/tasks.go b/backend/taskqueue/tasks.go
index 990ce65..cbe83b1 100644
--- a/backend/taskqueue/tasks.go
+++ b/backend/taskqueue/tasks.go
@@ -112,6 +112,8 @@ type TaskPayloadRunTestcase struct {
TaskPayloadBase
SubmissionID int
TestcaseID int
+ Stdin string
+ Stdout string
}
func newTaskRunTestcase(
@@ -120,6 +122,8 @@ func newTaskRunTestcase(
code string,
submissionID int,
testcaseID int,
+ stdin string,
+ stdout string,
) (*asynq.Task, error) {
payload, err := json.Marshal(TaskPayloadRunTestcase{
TaskPayloadBase: TaskPayloadBase{
@@ -129,6 +133,8 @@ func newTaskRunTestcase(
},
SubmissionID: submissionID,
TestcaseID: testcaseID,
+ Stdin: stdin,
+ Stdout: stdout,
})
if err != nil {
return nil, err