Skip to content

Commit c95625d

Browse files
committed
chore(project) : add controller for Project and make changes in jobQueue
1 parent c5206f6 commit c95625d

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/rabbitmq/jobqueue.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@ export interface RunJob {
2626
scenario: string
2727
}
2828

29-
export type JudgeJob = RunJob | SubmissionJob
29+
export interface ProjectJob {
30+
id: number
31+
source: string,
32+
lang: string,
33+
problem: string,
34+
timelimit: number,
35+
scenario: string
36+
}
37+
38+
export type JudgeJob = RunJob | SubmissionJob | ProjectJob
3039

3140
let jobQ = 'job_queue'
3241
let successQ = 'success_queue'
@@ -50,8 +59,17 @@ amqp.connect(`amqp://${config.AMQP.USER}:${config.AMQP.PASS}@${config.AMQP.HOST}
5059
jobChannel.consume(successQ, (msg) => {
5160
debug(`SUCCESS:CONSUME: msg.content = ${msg.content.toString()}`)
5261

53-
const payload = JSON.parse(msg.content.toString())
54-
const eventName = payload.testcases ? 'submit_result' : 'run_result'
62+
const payload = JSON.parse(msg.content.toString())
63+
let eventName;
64+
if (payload.testcases) {
65+
eventName = 'submit_result'
66+
}
67+
else if (payload.code && payload.score) {
68+
eventName = 'project_result'
69+
}
70+
else {
71+
eventName = 'run_result'
72+
}
5573

5674
successListener.emit(eventName, payload)
5775
jobChannel.ack(msg)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Request, Response } from 'express'
2+
import {ProjectJob, queueJob} from 'rabbitmq/jobqueue'
3+
import DB from 'models'
4+
import axios from 'axios'
5+
6+
type ProjectResponse = {
7+
id: number,
8+
stdout: string,
9+
stderr: string,
10+
time: number,
11+
code: number,
12+
score: number
13+
}
14+
type RunPoolElement = {
15+
res: Response
16+
}
17+
18+
const RunPool: { [x: number]: RunPoolElement } = {}
19+
20+
export default {
21+
async runPOST(req: Request, res: Response) {
22+
const mode = req.body.mode || 'sync'
23+
const job = await DB.submissions.create({
24+
lang: req.body.lang,
25+
start_time: new Date(),
26+
mode,
27+
callback: req.body.callback
28+
})
29+
30+
await queueJob(<ProjectJob>{
31+
id: job.id,
32+
source: req.body.submission,
33+
problem: req.body.problem,
34+
lang: req.body.lang,
35+
submissionDirs: req.body.submissionDirs,
36+
timelimit: req.body.timelimit,
37+
scenario: 'project'
38+
})
39+
40+
41+
if (['callback', 'poll'].includes(mode)) {
42+
return res.json({
43+
id: job.id
44+
})
45+
}
46+
47+
// if mode === 'sync'
48+
RunPool[job.id] = {
49+
res
50+
}
51+
},
52+
53+
async onSuccess(result: ProjectResponse) {
54+
const job = await DB.submissions.findById(result.id)
55+
job.results = result
56+
await job.save()
57+
58+
switch (job.mode) {
59+
case 'callback':
60+
await axios.post(job.callback, result)
61+
break
62+
case 'sync':
63+
RunPool[job.id].res.json(result)
64+
break
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)