From 296aa3f8a145a8fbc08db9f5b1d45fe6f72a38a4 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 5 Aug 2024 03:57:21 +0900 Subject: feat: implement task queue --- backend/taskqueue/processor.go | 25 +++++++++++++++++++++++++ backend/taskqueue/queue.go | 25 +++++++++++++++++++++++++ backend/taskqueue/tasks.go | 32 ++++++++++++++++++++++++++++++++ backend/taskqueue/worker_server.go | 27 +++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 backend/taskqueue/processor.go create mode 100644 backend/taskqueue/queue.go create mode 100644 backend/taskqueue/tasks.go create mode 100644 backend/taskqueue/worker_server.go (limited to 'backend/taskqueue') diff --git a/backend/taskqueue/processor.go b/backend/taskqueue/processor.go new file mode 100644 index 0000000..e26ac64 --- /dev/null +++ b/backend/taskqueue/processor.go @@ -0,0 +1,25 @@ +package taskqueue + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/hibiken/asynq" +) + +type ExecProcessor struct { +} + +func (processor *ExecProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error { + var payload TaskExecPlayload + if err := json.Unmarshal(t.Payload(), &payload); err != nil { + return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry) + } + // TODO + return nil +} + +func NewExecProcessor() *ExecProcessor { + return &ExecProcessor{} +} diff --git a/backend/taskqueue/queue.go b/backend/taskqueue/queue.go new file mode 100644 index 0000000..53ec6d6 --- /dev/null +++ b/backend/taskqueue/queue.go @@ -0,0 +1,25 @@ +package taskqueue + +import ( + "github.com/hibiken/asynq" +) + +type Queue struct { + client *asynq.Client +} + +func NewQueue(redisAddr string) *Queue { + return &Queue{ + client: asynq.NewClient(asynq.RedisClientOpt{ + Addr: redisAddr, + }), + } +} + +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...) +} diff --git a/backend/taskqueue/tasks.go b/backend/taskqueue/tasks.go new file mode 100644 index 0000000..2891a36 --- /dev/null +++ b/backend/taskqueue/tasks.go @@ -0,0 +1,32 @@ +package taskqueue + +import ( + "encoding/json" + + "github.com/hibiken/asynq" +) + +const ( + TaskTypeExec = "exec" +) + +type Testcase struct { + Stdin string + Stdout string +} + +type TaskExecPlayload struct { + Code string + Testcases []*Testcase +} + +func NewExecTask(code string, testcases []*Testcase) (*asynq.Task, error) { + payload, err := json.Marshal(TaskExecPlayload{ + Code: code, + Testcases: testcases, + }) + if err != nil { + return nil, err + } + return asynq.NewTask(TaskTypeExec, payload), nil +} diff --git a/backend/taskqueue/worker_server.go b/backend/taskqueue/worker_server.go new file mode 100644 index 0000000..9bdd81f --- /dev/null +++ b/backend/taskqueue/worker_server.go @@ -0,0 +1,27 @@ +package taskqueue + +import ( + "github.com/hibiken/asynq" +) + +type WorkerServer struct { + server *asynq.Server +} + +func NewWorkerServer(redisAddr string) *WorkerServer { + return &WorkerServer{ + server: asynq.NewServer( + asynq.RedisClientOpt{ + Addr: redisAddr, + }, + asynq.Config{}, + ), + } +} + +func (s *WorkerServer) Run() error { + mux := asynq.NewServeMux() + mux.Handle(TaskTypeExec, NewExecProcessor()) + + return s.server.Run(mux) +} -- cgit v1.2.3-70-g09d2