mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
升级queue为task
This commit is contained in:
1 parent
e7ce8c1f02
commit
5d7f57334f
14 files changed
+145
-130
No files matched your search
+12
-60
@@ -4,14 +4,12 @@ const problem = require('../model/problem');
|
||||
const builtin = require('../model/builtin');
|
||||
const contest = require('../model/contest');
|
||||
const user = require('../model/user');
|
||||
const task = require('../model/task');
|
||||
const bus = require('../service/bus');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
Route, Handler, Connection, ConnectionHandler,
|
||||
} = require('../service/server');
|
||||
|
||||
queue.assert('judge');
|
||||
|
||||
async function _postJudge(rdoc) {
|
||||
const accept = rdoc.status === builtin.STATUS_ACCEPTED;
|
||||
bus.publish('record_change', rdoc);
|
||||
@@ -57,6 +55,7 @@ async function next(body) {
|
||||
rdoc = await record.update(body.rid, $set, $push);
|
||||
bus.publish('record_change', rdoc);
|
||||
}
|
||||
|
||||
async function end(body) {
|
||||
let rdoc = await record.get(body.rid);
|
||||
const $set = {};
|
||||
@@ -92,34 +91,10 @@ class JudgeHandler extends Handler {
|
||||
async get({ check = false }) {
|
||||
if (check) return;
|
||||
const tasks = [];
|
||||
let rid = await queue.get('judge', false);
|
||||
while (rid) {
|
||||
const rdoc = await record.get(rid); // eslint-disable-line no-await-in-loop
|
||||
let task;
|
||||
if (!rdoc.input) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const pdoc = await problem.getById(rdoc.pid);
|
||||
task = {
|
||||
event: 'judge',
|
||||
rid,
|
||||
type: 0,
|
||||
pid: rdoc.pid,
|
||||
data: pdoc.data,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code,
|
||||
};
|
||||
} else {
|
||||
task = {
|
||||
event: 'pretest',
|
||||
rid,
|
||||
type: 0,
|
||||
input: rdoc.input,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code,
|
||||
};
|
||||
}
|
||||
tasks.push(task);
|
||||
rid = await queue.get('judge', false); // eslint-disable-line no-await-in-loop
|
||||
let t = await task.getFirst({ type: 'judge' });
|
||||
while (t) {
|
||||
tasks.push(t);
|
||||
t = await task.getFirst({ type: 'judge' }); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
this.response.body = { tasks };
|
||||
}
|
||||
@@ -133,6 +108,7 @@ class JudgeHandler extends Handler {
|
||||
await end(this.request.body);
|
||||
}
|
||||
}
|
||||
|
||||
class JudgeConnectionHandler extends ConnectionHandler {
|
||||
async prepare() {
|
||||
this.checkPerm(PERM_JUDGE);
|
||||
@@ -143,40 +119,16 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
||||
else if (msg.key === 'end') {
|
||||
await end({ judger: this.user._id, ...msg });
|
||||
this.processing = null;
|
||||
const rid = await queue.get('judge');
|
||||
const rdoc = await record.get(rid);
|
||||
let task;
|
||||
if (!rdoc.input) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const pdoc = await problem.getById(rdoc.pid);
|
||||
task = {
|
||||
event: 'judge',
|
||||
rid,
|
||||
type: 0,
|
||||
pid: rdoc.pid,
|
||||
data: pdoc.data,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code,
|
||||
};
|
||||
} else {
|
||||
task = {
|
||||
event: 'pretest',
|
||||
rid,
|
||||
type: 0,
|
||||
input: rdoc.input,
|
||||
lang: rdoc.lang,
|
||||
code: rdoc.code,
|
||||
};
|
||||
}
|
||||
this.send({ task });
|
||||
this.processing = task.rid;
|
||||
const t = await task.getFirst({ type: 'judge' });
|
||||
this.send({ task: t });
|
||||
this.processing = t;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
if (this.processing) {
|
||||
await record.reset(this.processing);
|
||||
queue.push('judge', this.processing);
|
||||
task.add(this.processing);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,5 +139,5 @@ async function apply() {
|
||||
}
|
||||
|
||||
global.Hydro.handler.judge = module.exports = {
|
||||
JudgeHandler, JudgeConnectionHandler, apply,
|
||||
JudgeHandler, JudgeConnectionHandler, apply, next, end,
|
||||
};
|
||||
@@ -11,7 +11,6 @@ const {
|
||||
Route, Connection, Handler, ConnectionHandler,
|
||||
} = require('../service/server');
|
||||
const gridfs = require('../service/gridfs');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
NoProblemError, ProblemDataNotFoundError, BadRequestError,
|
||||
SolutionNotFoundError,
|
||||
@@ -25,8 +24,6 @@ const {
|
||||
PERM_REPLY_PROBLEM_SOLUTION, PERM_LOGGEDIN,
|
||||
} = require('../permission');
|
||||
|
||||
queue.assert('judge');
|
||||
|
||||
class ProblemHandler extends Handler {
|
||||
async _prepare() {
|
||||
this.checkPerm(PERM_VIEW_PROBLEM);
|
||||
@@ -122,7 +119,7 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
|
||||
const rid = await record.add({
|
||||
uid: this.uid, lang, code, pid: this.pdoc._id,
|
||||
});
|
||||
await queue.push('judge', rid);
|
||||
await record.judge(rid);
|
||||
this.user.nSubmit++;
|
||||
this.response.body = { rid };
|
||||
this.response.redirect = `/r/${rid}`;
|
||||
@@ -135,7 +132,7 @@ class ProblemPretestHandler extends ProblemDetailHandler {
|
||||
const rid = await record.add({
|
||||
uid: this.uid, lang, code, pid: this.pdoc._id, input,
|
||||
});
|
||||
await queue.push('judge', rid);
|
||||
await record.judge(rid);
|
||||
this.response.body = { rid };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ const record = require('../model/record');
|
||||
const contest = require('../model/contest');
|
||||
const user = require('../model/user');
|
||||
const bus = require('../service/bus');
|
||||
const queue = require('../service/queue');
|
||||
const {
|
||||
Route, Handler, Connection, ConnectionHandler,
|
||||
} = require('../service/server');
|
||||
@@ -69,10 +68,7 @@ class RecordRejudgeHandler extends Handler {
|
||||
async post({ rid }) {
|
||||
this.checkPerm(PERM_REJUDGE);
|
||||
const rdoc = await record.get(rid);
|
||||
if (rdoc) {
|
||||
await record.reset(rid);
|
||||
await queue.push('judge', rid);
|
||||
}
|
||||
if (rdoc) await record.rejudge(rid);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user