judge: 优化测试数据同步流程

This commit is contained in:
undefined committed 2021-08-03 12:27:14 +08:00
1 parent 9a2b33e34d
commit d0592eac5b
3 files changed
+16 -3

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "@hydrooj/hydrojudge", "name": "@hydrooj/hydrojudge",
"bin": "bin/hydrojudge.js", "bin": "bin/hydrojudge.js",
"version": "2.9.3", "version": "2.9.4",
"main": "package.json", "main": "package.json",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",
+11 -2
View File
@@ -3,6 +3,7 @@ import path from 'path';
import axios from 'axios'; import axios from 'axios';
import fs from 'fs-extra'; import fs from 'fs-extra';
import WebSocket from 'ws'; import WebSocket from 'ws';
import PQueue from 'p-queue';
import cac from 'cac'; import cac from 'cac';
import { noop } from 'lodash'; import { noop } from 'lodash';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
@@ -212,13 +213,14 @@ export default class Hydro {
} }
if (filenames.length) { if (filenames.length) {
log.info(`Getting problem data: ${this.config.host}/${domainId}/${pid}`); log.info(`Getting problem data: ${this.config.host}/${domainId}/${pid}`);
if (next) next({ judge_text: '正在同步测试数据,请稍后' }); if (next) next({ judge_text: '正在同步测试数据,请稍候' });
await this.ensureLogin(); await this.ensureLogin();
const res = await this.axios.post(`/d/${domainId}/judge/files`, { const res = await this.axios.post(`/d/${domainId}/judge/files`, {
pid, pid,
files: filenames, files: filenames,
}); });
for (const name in res.data.links) { // eslint-disable-next-line no-inner-declarations
async function download(name: string) {
if (name.includes('/')) await fs.ensureDir(path.join(filePath, name.split('/')[0])); if (name.includes('/')) await fs.ensureDir(path.join(filePath, name.split('/')[0]));
const f = await this.axios.get(res.data.links[name], { responseType: 'stream' }); const f = await this.axios.get(res.data.links[name], { responseType: 'stream' });
const w = fs.createWriteStream(path.join(filePath, name)); const w = fs.createWriteStream(path.join(filePath, name));
@@ -228,6 +230,13 @@ export default class Hydro {
w.on('error', reject); w.on('error', reject);
}); });
} }
const tasks = [];
const queue = new PQueue({ concurrency: 10 });
for (const name in res.data.links) {
tasks.push(queue.add(() => download(name)));
}
queue.start();
await Promise.all(tasks);
fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version)); fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version));
await this.processData(filePath).catch(noop); await this.processData(filePath).catch(noop);
} }
+4
View File
@@ -88,6 +88,10 @@ function judgeCase(c, sid: string) {
if (code < 32) message = signals[code]; if (code < 32) message = signals[code];
else message = { message: 'Your program returned {0}.', params: [code] }; else message = { message: 'Your program returned {0}.', params: [code] };
} }
await Promise.all([
fs.remove(stdout),
fs.remove(stderr),
]).catch(() => { /* Ignore file doesn't exist */ });
ctxSubtask.score = Score[ctxSubtask.subtask.type](ctxSubtask.score, score); ctxSubtask.score = Score[ctxSubtask.subtask.type](ctxSubtask.score, score);
ctxSubtask.status = Math.max(ctxSubtask.status, status); ctxSubtask.status = Math.max(ctxSubtask.status, status);
ctx.total_time_usage_ms += time_usage_ms; ctx.total_time_usage_ms += time_usage_ms;