mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: 优化任务分配逻辑
This commit is contained in:
1 parent
cafb3b03e5
commit
5671eb0843
3 files changed
+30
-5
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.13.49",
|
"version": "2.13.50",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "dist/loader.js",
|
"main": "dist/loader.js",
|
||||||
"typings": "dist/loader.d.ts",
|
"typings": "dist/loader.d.ts",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { ObjectID } from 'mongodb';
|
import { ObjectID } from 'mongodb';
|
||||||
import { JudgeResultBody, Rdoc, TestCase } from '../interface';
|
import { JudgeResultBody, Rdoc, TestCase } from '../interface';
|
||||||
|
import { sleep } from '../utils';
|
||||||
import * as record from '../model/record';
|
import * as record from '../model/record';
|
||||||
import * as problem from '../model/problem';
|
import * as problem from '../model/problem';
|
||||||
import * as builtin from '../model/builtin';
|
import * as builtin from '../model/builtin';
|
||||||
@@ -113,16 +114,32 @@ class JudgeHandler extends Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class JudgeConnectionHandler extends ConnectionHandler {
|
class JudgeConnectionHandler extends ConnectionHandler {
|
||||||
processing: any;
|
processing: any = null;
|
||||||
|
|
||||||
|
async prepare() {
|
||||||
|
this.newTask();
|
||||||
|
}
|
||||||
|
|
||||||
|
async newTask() {
|
||||||
|
if (this.processing) return;
|
||||||
|
let t;
|
||||||
|
while (!t) {
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
await sleep(100);
|
||||||
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
t = await task.getFirst({ type: 'judge' });
|
||||||
|
}
|
||||||
|
this.send({ task: t });
|
||||||
|
this.processing = t;
|
||||||
|
}
|
||||||
|
|
||||||
async message(msg) {
|
async message(msg) {
|
||||||
|
console.log(msg);
|
||||||
if (msg.key === 'next') await next(msg);
|
if (msg.key === 'next') await next(msg);
|
||||||
else if (msg.key === 'end') {
|
else if (msg.key === 'end') {
|
||||||
await end({ judger: this.user._id, ...msg });
|
await end({ judger: this.user._id, ...msg });
|
||||||
this.processing = null;
|
this.processing = null;
|
||||||
const t = await task.getFirst({ type: 'judge' });
|
await this.newTask();
|
||||||
this.send({ task: t });
|
|
||||||
this.processing = t;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -193,3 +193,11 @@ export function buildProjection(fields: string[]): Record<string, 1> {
|
|||||||
for (const k of fields) o[k] = 1;
|
for (const k of fields) o[k] = 1;
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sleep(timeout: number) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve();
|
||||||
|
}, timeout);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user