diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-05 03:57:21 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-05 03:57:21 +0900 |
| commit | 296aa3f8a145a8fbc08db9f5b1d45fe6f72a38a4 (patch) | |
| tree | 11180e08e96e1adb492b3aa52d32edd79a669711 /backend/taskqueue | |
| parent | 9ff9c151e5defd9eed5cba3c88bc341b4360d09c (diff) | |
| download | phperkaigi-2025-albatross-296aa3f8a145a8fbc08db9f5b1d45fe6f72a38a4.tar.gz phperkaigi-2025-albatross-296aa3f8a145a8fbc08db9f5b1d45fe6f72a38a4.tar.zst phperkaigi-2025-albatross-296aa3f8a145a8fbc08db9f5b1d45fe6f72a38a4.zip | |
feat: implement task queue
Diffstat (limited to 'backend/taskqueue')
| -rw-r--r-- | backend/taskqueue/processor.go | 25 | ||||
| -rw-r--r-- | backend/taskqueue/queue.go | 25 | ||||
| -rw-r--r-- | backend/taskqueue/tasks.go | 32 | ||||
| -rw-r--r-- | backend/taskqueue/worker_server.go | 27 |
4 files changed, 109 insertions, 0 deletions
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) +} |
