core: judge: optimize task assign (#718)

This commit is contained in:
Yang Gao authored and GitHub committed 2024-01-24 10:28:25 +08:00
1 parent cffce0bd79
commit 19c3d9e95c
3 files changed
+13 -2

No files matched your search

+1
View File
@@ -224,6 +224,7 @@ export default class Hydro implements Session {
this.ws.send(JSON.stringify({ key: 'status', info: { mid, ...inf } })); this.ws.send(JSON.stringify({ key: 'status', info: { mid, ...inf } }));
}, 1200000); }, 1200000);
} }
this.ws.send(content);
resolve(null); resolve(null);
}); });
}); });
+10 -2
View File
@@ -217,6 +217,7 @@ class JudgeConnectionHandler extends ConnectionHandler {
query: any = { type: { $in: ['judge', 'generate'] } }; query: any = { type: { $in: ['judge', 'generate'] } };
rdocs: Record<string, RecordDoc> = {}; rdocs: Record<string, RecordDoc> = {};
concurrency = 1; concurrency = 1;
callback: (res?: any) => void = null;
async prepare() { async prepare() {
logger.info('Judge daemon connected from ', this.request.ip); logger.info('Judge daemon connected from ', this.request.ip);
@@ -235,7 +236,12 @@ class JudgeConnectionHandler extends ConnectionHandler {
while (!this.closed) { while (!this.closed) {
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
if (this.processing.length >= this.concurrency) { if (this.processing.length >= this.concurrency) {
await sleep(500); await Promise.race([
sleep(500),
new Promise((resolve) => {
this.callback = resolve;
}),
]);
continue; continue;
} }
const t = await task.getFirst(this.query); const t = await task.getFirst(this.query);
@@ -264,9 +270,10 @@ class JudgeConnectionHandler extends ConnectionHandler {
if (!rdoc) return; if (!rdoc) return;
if (msg.key === 'next') await next({ ...msg, domainId: rdoc.domainId }); if (msg.key === 'next') await next({ ...msg, domainId: rdoc.domainId });
if (msg.key === 'end') { if (msg.key === 'end') {
if (!msg.nop) await end({ judger: this.user._id, ...msg, domainId: rdoc.domainId }).catch((e) => logger.error(e));
this.processing = this.processing.filter((t) => t.rid.toHexString() !== msg.rid); this.processing = this.processing.filter((t) => t.rid.toHexString() !== msg.rid);
delete this.rdocs[msg.rid]; delete this.rdocs[msg.rid];
this.callback?.();
if (!msg.nop) await end({ judger: this.user._id, ...msg, domainId: rdoc.domainId }).catch((e) => logger.error(e));
} }
} else if (msg.key === 'status') { } else if (msg.key === 'status') {
await updateJudge(msg.info); await updateJudge(msg.info);
@@ -279,6 +286,7 @@ class JudgeConnectionHandler extends ConnectionHandler {
} }
if (Number.isSafeInteger(msg.concurrency) && msg.concurrency > 0) { if (Number.isSafeInteger(msg.concurrency) && msg.concurrency > 0) {
this.concurrency = msg.concurrency; this.concurrency = msg.concurrency;
this.callback?.();
} }
if (msg.lang instanceof Array && msg.lang.every((i) => typeof i === 'string')) { if (msg.lang instanceof Array && msg.lang.every((i) => typeof i === 'string')) {
this.query.lang = { $in: msg.lang }; this.query.lang = { $in: msg.lang };
+2
View File
@@ -417,6 +417,7 @@ export function Connection(
h.compression = new Shorty(); h.compression = new Shorty();
conn.send('shorty'); conn.send('shorty');
} }
conn.pause();
if (h._prepare) await h._prepare(args); if (h._prepare) await h._prepare(args);
if (h.prepare) await h.prepare(args); if (h.prepare) await h.prepare(args);
// eslint-disable-next-line @typescript-eslint/no-shadow // eslint-disable-next-line @typescript-eslint/no-shadow
@@ -449,6 +450,7 @@ export function Connection(
h.message?.(JSON.parse(e.data.toString())); h.message?.(JSON.parse(e.data.toString()));
}; };
conn.onclose = clean; conn.onclose = clean;
conn.resume();
await bus.parallel('connection/active', h); await bus.parallel('connection/active', h);
} catch (e) { } catch (e) {
await h.onerror(e); await h.onerror(e);