From 5671eb084308738ae09be339f31c4266025cbd01 Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 11 Nov 2020 13:21:57 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E4=BC=98=E5=8C=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=88=86=E9=85=8D=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/hydrooj/package.json | 2 +- packages/hydrooj/src/handler/judge.ts | 25 +++++++++++++++++++++---- packages/hydrooj/src/utils.ts | 8 ++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index 6b1f8cdbb..43a893097 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.13.49", + "version": "2.13.50", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/handler/judge.ts b/packages/hydrooj/src/handler/judge.ts index 73ea46091..14833ddae 100644 --- a/packages/hydrooj/src/handler/judge.ts +++ b/packages/hydrooj/src/handler/judge.ts @@ -1,5 +1,6 @@ import { ObjectID } from 'mongodb'; import { JudgeResultBody, Rdoc, TestCase } from '../interface'; +import { sleep } from '../utils'; import * as record from '../model/record'; import * as problem from '../model/problem'; import * as builtin from '../model/builtin'; @@ -113,16 +114,32 @@ class JudgeHandler extends Handler { } 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) { + console.log(msg); if (msg.key === 'next') await next(msg); else if (msg.key === 'end') { await end({ judger: this.user._id, ...msg }); this.processing = null; - const t = await task.getFirst({ type: 'judge' }); - this.send({ task: t }); - this.processing = t; + await this.newTask(); } } diff --git a/packages/hydrooj/src/utils.ts b/packages/hydrooj/src/utils.ts index 311ce5bad..815d2456d 100644 --- a/packages/hydrooj/src/utils.ts +++ b/packages/hydrooj/src/utils.ts @@ -193,3 +193,11 @@ export function buildProjection(fields: string[]): Record { for (const k of fields) o[k] = 1; return o; } + +export function sleep(timeout: number) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, timeout); + }); +}