aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/taskqueue/tasks.go
blob: 943943f237aa87b65a349220f44c7e703a564d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package taskqueue

import (
	"encoding/json"

	"github.com/hibiken/asynq"
)

type TaskType string

const (
	TaskTypeRunTestcase TaskType = "run_testcase"
)

type TaskPayloadRunTestcase struct {
	GameID       int
	UserID       int
	SubmissionID int
	TestcaseID   int
	Language     string
	Code         string
	Stdin        string
	Stdout       string
}

func newTaskRunTestcase(
	gameID int,
	userID int,
	submissionID int,
	testcaseID int,
	language string,
	code string,
	stdin string,
	stdout string,
) (*asynq.Task, error) {
	payload, err := json.Marshal(TaskPayloadRunTestcase{
		GameID:       gameID,
		UserID:       userID,
		SubmissionID: submissionID,
		TestcaseID:   testcaseID,
		Language:     language,
		Code:         code,
		Stdin:        stdin,
		Stdout:       stdout,
	})
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(
		string(TaskTypeRunTestcase),
		payload,
		asynq.MaxRetry(3),
	), nil
}

type TaskResult interface {
	Type() TaskType
	GameID() int
}

type TaskResultRunTestcase struct {
	TaskPayload *TaskPayloadRunTestcase
	Status      string
	Stdout      string
	Stderr      string
	Err         error
}

func (r *TaskResultRunTestcase) Type() TaskType { return TaskTypeRunTestcase }
func (r *TaskResultRunTestcase) GameID() int    { return r.TaskPayload.GameID }