mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: judge: optimize task assign
This commit is contained in:
1 file changed
+19
-25
@@ -218,7 +218,6 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
closed = false;
|
closed = false;
|
||||||
query: any = { type: { $in: ['judge', 'generate'] } };
|
query: any = { type: { $in: ['judge', 'generate'] } };
|
||||||
rdocs: Record<string, RecordDoc> = {};
|
rdocs: Record<string, RecordDoc> = {};
|
||||||
ip: string;
|
|
||||||
concurrency = 1;
|
concurrency = 1;
|
||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
@@ -226,7 +225,7 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
this.sendLanguageConfig();
|
this.sendLanguageConfig();
|
||||||
// Ensure language sent
|
// Ensure language sent
|
||||||
await sleep(100);
|
await sleep(100);
|
||||||
this.newTask();
|
this.newTask().catch((e) => logger.error(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
@subscribe('system/setting')
|
@subscribe('system/setting')
|
||||||
@@ -235,24 +234,25 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async newTask() {
|
async newTask() {
|
||||||
if (this.processing.length >= this.concurrency) return;
|
while (!this.closed) {
|
||||||
let t;
|
|
||||||
let rdoc: RecordDoc;
|
|
||||||
while (!t) {
|
|
||||||
if (this.closed) return;
|
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
t = await task.getFirst(this.query);
|
if (this.processing.length >= this.concurrency) {
|
||||||
if (!t) await sleep(500);
|
await sleep(500);
|
||||||
else rdoc = await record.get(t.domainId, t.rid);
|
continue;
|
||||||
/* eslint-enable no-await-in-loop */
|
}
|
||||||
if (!rdoc) t = null;
|
const t = await task.getFirst(this.query);
|
||||||
|
if (!t) {
|
||||||
|
await sleep(500);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const rdoc = await record.get(t.domainId, t.rid);
|
||||||
|
if (!rdoc) continue;
|
||||||
|
|
||||||
|
this.send({ task: { ...rdoc, ...t } });
|
||||||
|
this.rdocs[rdoc._id.toHexString()] = rdoc;
|
||||||
|
this.processing.push(t);
|
||||||
|
await next({ status: STATUS.STATUS_FETCHED, domainId: rdoc.domainId, rid: rdoc._id });
|
||||||
}
|
}
|
||||||
this.send({ task: { ...rdoc, ...t } });
|
|
||||||
this.rdocs[rdoc._id.toHexString()] = rdoc;
|
|
||||||
this.processing.push(t);
|
|
||||||
const $set = { status: builtin.STATUS.STATUS_FETCHED };
|
|
||||||
rdoc = await record.update(t.domainId, t.rid, $set, {});
|
|
||||||
bus.broadcast('record/change', rdoc, $set, {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async message(msg) {
|
async message(msg) {
|
||||||
@@ -269,24 +269,18 @@ class JudgeConnectionHandler extends ConnectionHandler {
|
|||||||
if (!msg.nop) await end({ judger: this.user._id, ...msg, domainId: rdoc.domainId }).catch((e) => logger.error(e));
|
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];
|
||||||
await this.newTask();
|
|
||||||
}
|
}
|
||||||
} else if (msg.key === 'status') {
|
} else if (msg.key === 'status') {
|
||||||
await updateJudge(msg.info);
|
await updateJudge(msg.info);
|
||||||
} else if (msg.key === 'prio' && typeof msg.prio === 'number') {
|
} else if (msg.key === 'prio' && typeof msg.prio === 'number') {
|
||||||
|
// TODO deprecated, use config instead
|
||||||
this.query.priority = { $gt: msg.prio };
|
this.query.priority = { $gt: msg.prio };
|
||||||
} else if (msg.key === 'config') {
|
} else if (msg.key === 'config') {
|
||||||
if (Number.isSafeInteger(msg.prio)) {
|
if (Number.isSafeInteger(msg.prio)) {
|
||||||
this.query.priority = { $gt: msg.prio };
|
this.query.priority = { $gt: msg.prio };
|
||||||
}
|
}
|
||||||
if (Number.isSafeInteger(msg.concurrency) && msg.concurrency > 0) {
|
if (Number.isSafeInteger(msg.concurrency) && msg.concurrency > 0) {
|
||||||
const old = this.concurrency;
|
|
||||||
this.concurrency = msg.concurrency;
|
this.concurrency = msg.concurrency;
|
||||||
if (old <= this.concurrency) {
|
|
||||||
for (let i = old; i < this.concurrency; i++) {
|
|
||||||
await this.newTask(); // eslint-disable-line no-await-in-loop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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 };
|
||||||
|
|||||||
Reference in new issue
Block a user