diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-08 01:28:43 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-08 04:02:37 +0900 |
| commit | 26378c2d2ad1f8cb3f20c7070be3be9a4f0a0ad6 (patch) | |
| tree | 56f9359013760b67abaaf598de5acbc62b89c9a3 | |
| parent | d936d587df9d96f373cf8ec35f0c6c6882083f32 (diff) | |
| download | iosdc-japan-2025-albatross-26378c2d2ad1f8cb3f20c7070be3be9a4f0a0ad6.tar.gz iosdc-japan-2025-albatross-26378c2d2ad1f8cb3f20c7070be3be9a4f0a0ad6.tar.zst iosdc-japan-2025-albatross-26378c2d2ad1f8cb3f20c7070be3be9a4f0a0ad6.zip | |
refactor(backend): add Queue.Enqueue*() variants
| -rw-r--r-- | backend/game/hub.go | 9 | ||||
| -rw-r--r-- | backend/taskqueue/queue.go | 78 | ||||
| -rw-r--r-- | backend/taskqueue/tasks.go | 8 |
3 files changed, 84 insertions, 11 deletions
diff --git a/backend/game/hub.go b/backend/game/hub.go index 58eb180..b51d977 100644 --- a/backend/game/hub.go +++ b/backend/game/hub.go @@ -162,16 +162,15 @@ func (hub *gameHub) run() { log.Printf("submit: %v", message.message) code := msg.Data.Code codeSize := len(code) // TODO: exclude whitespaces. - task, err := taskqueue.NewTaskCreateSubmissionRecord( + if err := hub.taskQueue.EnqueueTaskCreateSubmissionRecord( hub.game.gameID, message.client.playerID, code, codeSize, - ) - if err != nil { - log.Fatalf("failed to create task: %v", err) + ); err != nil { + // TODO: notify failure to player + log.Fatalf("failed to enqueue task: %v", err) } - hub.taskQueue.Enqueue(task) default: log.Printf("unexpected message type: %T", message.message) } diff --git a/backend/taskqueue/queue.go b/backend/taskqueue/queue.go index 53ec6d6..b7d7381 100644 --- a/backend/taskqueue/queue.go +++ b/backend/taskqueue/queue.go @@ -20,6 +20,80 @@ func (q *Queue) Close() { q.client.Close() } -func (q *Queue) Enqueue(task *asynq.Task, opts ...asynq.Option) (*asynq.TaskInfo, error) { - return q.client.Enqueue(task, opts...) +func (q *Queue) EnqueueTaskCreateSubmissionRecord( + gameID int, + userID int, + code string, + codeSize int, +) error { + task, err := newTaskCreateSubmissionRecord( + gameID, + userID, + code, + codeSize, + ) + if err != nil { + return err + } + _, err = q.client.Enqueue(task) + return err +} + +func (q *Queue) EnqueueTaskCompileSwiftToWasm( + gameID int, + userID int, + code string, + submissionID int, +) error { + task, err := newTaskCompileSwiftToWasm( + gameID, + userID, + code, + submissionID, + ) + if err != nil { + return err + } + _, err = q.client.Enqueue(task) + return err +} + +func (q *Queue) EnqueueTaskCompileWasmToNativeExecutable( + gameID int, + userID int, + code string, + submissionID int, +) error { + task, err := newTaskCompileWasmToNativeExecutable( + gameID, + userID, + code, + submissionID, + ) + if err != nil { + return err + } + _, err = q.client.Enqueue(task) + return err +} + +func (q *Queue) EnqueueTaskRunTestcase( + gameID int, + userID int, + code string, + submissionID int, + testcaseID int, +) error { + task, err := newTaskRunTestcase( + gameID, + userID, + code, + submissionID, + testcaseID, + ) + if err != nil { + return err + } + _, err = q.client.Enqueue(task) + return err } diff --git a/backend/taskqueue/tasks.go b/backend/taskqueue/tasks.go index 679b6a0..990ce65 100644 --- a/backend/taskqueue/tasks.go +++ b/backend/taskqueue/tasks.go @@ -26,7 +26,7 @@ type TaskPayloadCreateSubmissionRecord struct { CodeSize int } -func NewTaskCreateSubmissionRecord( +func newTaskCreateSubmissionRecord( gameID int, userID int, code string, @@ -55,7 +55,7 @@ type TaskPayloadCompileSwiftToWasm struct { SubmissionID int } -func NewTaskCompileSwiftToWasm( +func newTaskCompileSwiftToWasm( gameID int, userID int, code string, @@ -84,7 +84,7 @@ type TaskPayloadCompileWasmToNativeExecutable struct { SubmissionID int } -func NewTaskCompileWasmToNativeExecutable( +func newTaskCompileWasmToNativeExecutable( gameID int, userID int, code string, @@ -114,7 +114,7 @@ type TaskPayloadRunTestcase struct { TestcaseID int } -func NewTaskRunTestcase( +func newTaskRunTestcase( gameID int, userID int, code string, |
