aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/taskqueue/tasks.go
blob: d5f2993b906a91d9d0fcf2d02124c38e9cc857ab (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package taskqueue

import (
	"encoding/json"

	"github.com/hibiken/asynq"
)

type TaskType string

// MD5 hash in hexadecimal format
type MD5HexHash string

const (
	TaskTypeCreateSubmissionRecord        TaskType = "create_submission_record"
	TaskTypeCompileSwiftToWasm            TaskType = "compile_swift_to_wasm"
	TaskTypeCompileWasmToNativeExecutable TaskType = "compile_wasm_to_native_executable"
	TaskTypeRunTestcase                   TaskType = "run_testcase"
)

type TaskPayloadBase struct {
	GameID   int
	UserID   int
	CodeHash MD5HexHash
}

type TaskPayloadCreateSubmissionRecord struct {
	TaskPayloadBase
	Code     string
	CodeSize int
}

func newTaskCreateSubmissionRecord(
	gameID int,
	userID int,
	code string,
	codeSize int,
	codeHash MD5HexHash,
) (*asynq.Task, error) {
	payload, err := json.Marshal(TaskPayloadCreateSubmissionRecord{
		TaskPayloadBase: TaskPayloadBase{
			GameID:   gameID,
			UserID:   userID,
			CodeHash: codeHash,
		},
		Code:     code,
		CodeSize: codeSize,
	})
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(string(TaskTypeCreateSubmissionRecord), payload), nil
}

func (t *TaskPayloadCreateSubmissionRecord) GameID() int          { return t.TaskPayloadBase.GameID }
func (t *TaskPayloadCreateSubmissionRecord) UserID() int          { return t.TaskPayloadBase.UserID }
func (t *TaskPayloadCreateSubmissionRecord) CodeHash() MD5HexHash { return t.TaskPayloadBase.CodeHash }

type TaskPayloadCompileSwiftToWasm struct {
	TaskPayloadBase
	SubmissionID int
	Code         string
}

func newTaskCompileSwiftToWasm(
	gameID int,
	userID int,
	code string,
	codeHash MD5HexHash,
	submissionID int,
) (*asynq.Task, error) {
	payload, err := json.Marshal(TaskPayloadCompileSwiftToWasm{
		TaskPayloadBase: TaskPayloadBase{
			GameID:   gameID,
			UserID:   userID,
			CodeHash: codeHash,
		},
		SubmissionID: submissionID,
		Code:         code,
	})
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(string(TaskTypeCompileSwiftToWasm), payload), nil
}

func (t *TaskPayloadCompileSwiftToWasm) GameID() int          { return t.TaskPayloadBase.GameID }
func (t *TaskPayloadCompileSwiftToWasm) UserID() int          { return t.TaskPayloadBase.UserID }
func (t *TaskPayloadCompileSwiftToWasm) CodeHash() MD5HexHash { return t.TaskPayloadBase.CodeHash }

type TaskPayloadCompileWasmToNativeExecutable struct {
	TaskPayloadBase
	SubmissionID int
}

func newTaskCompileWasmToNativeExecutable(
	gameID int,
	userID int,
	codeHash MD5HexHash,
	submissionID int,
) (*asynq.Task, error) {
	payload, err := json.Marshal(TaskPayloadCompileWasmToNativeExecutable{
		TaskPayloadBase: TaskPayloadBase{
			GameID:   gameID,
			UserID:   userID,
			CodeHash: codeHash,
		},
		SubmissionID: submissionID,
	})
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(string(TaskTypeCompileWasmToNativeExecutable), payload), nil
}

func (t *TaskPayloadCompileWasmToNativeExecutable) GameID() int { return t.TaskPayloadBase.GameID }
func (t *TaskPayloadCompileWasmToNativeExecutable) UserID() int { return t.TaskPayloadBase.UserID }
func (t *TaskPayloadCompileWasmToNativeExecutable) CodeHash() MD5HexHash {
	return t.TaskPayloadBase.CodeHash
}

type TaskPayloadRunTestcase struct {
	TaskPayloadBase
	SubmissionID int
	TestcaseID   int
	Stdin        string
	Stdout       string
}

func newTaskRunTestcase(
	gameID int,
	userID int,
	codeHash MD5HexHash,
	submissionID int,
	testcaseID int,
	stdin string,
	stdout string,
) (*asynq.Task, error) {
	payload, err := json.Marshal(TaskPayloadRunTestcase{
		TaskPayloadBase: TaskPayloadBase{
			GameID:   gameID,
			UserID:   userID,
			CodeHash: codeHash,
		},
		SubmissionID: submissionID,
		TestcaseID:   testcaseID,
		Stdin:        stdin,
		Stdout:       stdout,
	})
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(string(TaskTypeRunTestcase), payload), nil
}

func (t *TaskPayloadRunTestcase) GameID() int          { return t.TaskPayloadBase.GameID }
func (t *TaskPayloadRunTestcase) UserID() int          { return t.TaskPayloadBase.UserID }
func (t *TaskPayloadRunTestcase) CodeHash() MD5HexHash { return t.TaskPayloadBase.CodeHash }

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

type TaskResultCreateSubmissionRecord struct {
	TaskPayload  *TaskPayloadCreateSubmissionRecord
	SubmissionID int
	Err          error
}

func (r *TaskResultCreateSubmissionRecord) Type() TaskType { return TaskTypeCreateSubmissionRecord }
func (r *TaskResultCreateSubmissionRecord) GameID() int    { return r.TaskPayload.GameID() }

type TaskResultCompileSwiftToWasm struct {
	TaskPayload *TaskPayloadCompileSwiftToWasm
	Status      string
	Stdout      string
	Stderr      string
	Err         error
}

func (r *TaskResultCompileSwiftToWasm) Type() TaskType { return TaskTypeCompileSwiftToWasm }
func (r *TaskResultCompileSwiftToWasm) GameID() int    { return r.TaskPayload.GameID() }

type TaskResultCompileWasmToNativeExecutable struct {
	TaskPayload *TaskPayloadCompileWasmToNativeExecutable
	Status      string
	Stdout      string
	Stderr      string
	Err         error
}

func (r *TaskResultCompileWasmToNativeExecutable) Type() TaskType {
	return TaskTypeCompileWasmToNativeExecutable
}
func (r *TaskResultCompileWasmToNativeExecutable) GameID() int { return r.TaskPayload.GameID() }

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() }