judge: remove low-performance api

This commit is contained in:
undefined committed 2022-09-06 13:02:46 +08:00
1 parent 9d0aeebe12
commit ce8627bce8
6 files changed
+36 -49

No files matched your search

+4 -4
View File
@@ -49,7 +49,7 @@ export async function processTestdata(folder: string) {
} }
const ini = files.filter((i) => i.toLowerCase() === 'config.ini')[0]; const ini = files.filter((i) => i.toLowerCase() === 'config.ini')[0];
if (!ini) return; if (!ini) return;
const t = fs.readFileSync(path.resolve(folder, ini), 'utf8'); const t = await fs.readFile(path.resolve(folder, ini), 'utf8');
await fs.writeFile(path.resolve(folder, 'config.ini'), t.toLowerCase()); await fs.writeFile(path.resolve(folder, 'config.ini'), t.toLowerCase());
for (const i of files) { for (const i of files) {
if (i.toLowerCase() === 'input') await fs.rename(`${folder}/${i}`, `${folder}/input`); if (i.toLowerCase() === 'input') await fs.rename(`${folder}/${i}`, `${folder}/input`);
@@ -84,11 +84,11 @@ export default async function readCases(folder: string, cfg: ProblemConfigFile =
}; };
try { try {
if (fs.existsSync(yamlConfig)) { if (fs.existsSync(yamlConfig)) {
Object.assign(config, yaml.load(fs.readFileSync(yamlConfig).toString())); Object.assign(config, yaml.load(await fs.readFile(yamlConfig, 'utf-8')));
} else if (fs.existsSync(ymlConfig)) { } else if (fs.existsSync(ymlConfig)) {
Object.assign(config, yaml.load(fs.readFileSync(ymlConfig).toString())); Object.assign(config, yaml.load(await fs.readFile(ymlConfig, 'utf-8')));
} else if (fs.existsSync(iniConfig)) { } else if (fs.existsSync(iniConfig)) {
Object.assign(config, convertIniConfig(fs.readFileSync(iniConfig).toString())); Object.assign(config, convertIniConfig(await fs.readFile(iniConfig, 'utf-8')));
} }
} catch (e) { } catch (e) {
throw changeErrorType(e, FormatError); throw changeErrorType(e, FormatError);
+5 -3
View File
@@ -53,7 +53,7 @@ const session = {
if (!files?.length) throw new FormatError('Problem data not found.'); if (!files?.length) throw new FormatError('Problem data not found.');
let etags: Record<string, string> = {}; let etags: Record<string, string> = {};
try { try {
etags = JSON.parse(fs.readFileSync(path.join(filePath, 'etags')).toString()); etags = JSON.parse(await fs.readFile(path.join(filePath, 'etags'), 'utf-8'));
} catch (e) { /* ignore */ } } catch (e) { /* ignore */ }
const version = {}; const version = {};
const filenames = new Set<string>(); const filenames = new Set<string>();
@@ -67,8 +67,10 @@ const session = {
for (const name in etags) { for (const name in etags) {
if (!filenames.has(name) && fs.existsSync(path.join(filePath, name))) await fs.remove(path.join(filePath, name)); if (!filenames.has(name) && fs.existsSync(path.join(filePath, name))) await fs.remove(path.join(filePath, name));
} }
fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version)); await Promise.all([
fs.writeFileSync(path.join(filePath, 'lastUsage'), Date.now().toString()); fs.writeFile(path.join(filePath, 'etags'), JSON.stringify(version)),
fs.writeFile(path.join(filePath, 'lastUsage'), Date.now().toString()),
]);
await processTestdata(filePath); await processTestdata(filePath);
return filePath; return filePath;
}, },
+3 -3
View File
@@ -64,7 +64,7 @@ export default class Hydro {
if (!files?.length) throw new FormatError('Problem data not found.'); if (!files?.length) throw new FormatError('Problem data not found.');
let etags: Record<string, string> = {}; let etags: Record<string, string> = {};
try { try {
etags = JSON.parse(fs.readFileSync(path.join(filePath, 'etags')).toString()); etags = JSON.parse(await fs.readFile(path.join(filePath, 'etags'), 'utf-8'));
} catch (e) { /* ignore */ } } catch (e) { /* ignore */ }
const version = {}; const version = {};
const filenames = []; const filenames = [];
@@ -102,10 +102,10 @@ export default class Hydro {
} }
queue.start(); queue.start();
await Promise.all(tasks); await Promise.all(tasks);
fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version)); await fs.writeFile(path.join(filePath, 'etags'), JSON.stringify(version));
await processTestdata(filePath); await processTestdata(filePath);
} }
fs.writeFileSync(path.join(filePath, 'lastUsage'), new Date().getTime().toString()); await fs.writeFile(path.join(filePath, 'lastUsage'), new Date().getTime().toString());
return filePath; return filePath;
} }
+1 -2
View File
@@ -68,8 +68,7 @@ export const judge = async (ctx: Context) => {
if (code < 32) message.push(`ExitCode: ${code} (${signals[code]})`); if (code < 32) message.push(`ExitCode: ${code} (${signals[code]})`);
else message.push(`ExitCode: ${code}`); else message.push(`ExitCode: ${code}`);
} }
message.push(res.stdout); message.push(res.stdout, res.stderr);
message.push(res.stderr);
ctx.next({ ctx.next({
status, status,
case: { case: {
+3 -8
View File
@@ -1,5 +1,4 @@
import cac from 'cac'; import cac from 'cac';
import fs from 'fs-extra';
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import { ParseEntry } from 'shell-quote'; import { ParseEntry } from 'shell-quote';
import { STATUS } from '@hydrooj/utils/lib/status'; import { STATUS } from '@hydrooj/utils/lib/status';
@@ -31,8 +30,6 @@ const statusMap: Map<SandboxStatus, number> = new Map([
interface Parameter { interface Parameter {
time?: number; time?: number;
stdin?: CopyInFile; stdin?: CopyInFile;
stdout?: string;
stderr?: string;
execute?: string; execute?: string;
memory?: number; memory?: number;
processLimit?: number; processLimit?: number;
@@ -78,7 +75,7 @@ function proc(params: Parameter): Cmd {
const size = parseMemoryMB(getConfig('stdio_size')); const size = parseMemoryMB(getConfig('stdio_size'));
const rate = getConfig('rate'); const rate = getConfig('rate');
const copyOutCached = [...(params.copyOutCached || [])]; const copyOutCached = [...(params.copyOutCached || [])];
if (params.cacheStdoutAndStderr) copyOutCached.push(params.filename ? `${params.filename}.out` : 'stdout', 'stderr'); if (params.cacheStdoutAndStderr) copyOutCached.push(params.filename ? `${params.filename}.out?` : 'stdout', 'stderr');
const copyIn = { ...(params.copyIn || {}) }; const copyIn = { ...(params.copyIn || {}) };
const stdin = params.stdin || { content: '' }; const stdin = params.stdin || { content: '' };
if (params.filename) copyIn[`${params.filename}.in`] = stdin; if (params.filename) copyIn[`${params.filename}.in`] = stdin;
@@ -121,10 +118,8 @@ async function adaptResult(result: SandboxResult, params: Parameter): Promise<Sa
ret.files = result.files || {}; ret.files = result.files || {};
ret.fileIds = result.fileIds || {}; ret.fileIds = result.fileIds || {};
if (ret.fileIds[outname]) ret.fileIds.stdout = ret.fileIds[outname]; if (ret.fileIds[outname]) ret.fileIds.stdout = ret.fileIds[outname];
if (params.stdout) await fs.writeFile(params.stdout, ret.files[outname] || ''); ret.stdout = ret.files[outname] || '';
else ret.stdout = ret.files[outname] || ''; ret.stderr = ret.files.stderr || result.error || '';
if (params.stderr) await fs.writeFile(params.stderr, ret.files.stderr || result.error || '');
else ret.stderr = ret.files.stderr || result.error || '';
if (result.error) ret.error = result.error; if (result.error) ret.error = result.error;
return ret; return ret;
} }
+20 -29
View File
@@ -2,38 +2,29 @@ import superagent from 'superagent';
import { getConfig } from '../config'; import { getConfig } from '../config';
import { SandboxRequest, SandboxResult, SandboxVersion } from './interface'; import { SandboxRequest, SandboxResult, SandboxVersion } from './interface';
class SandboxClient { let url;
constructor(public baseURL: string) {
if (baseURL.endsWith('/')) this.baseURL = baseURL.substring(0, baseURL.length - 1);
}
public run(req: SandboxRequest): Promise<SandboxResult[]> { const client = new Proxy({
return superagent.post(`${this.baseURL}/run`).send(req).then((res) => res.body); run(req: SandboxRequest): Promise<SandboxResult[]> {
} return superagent.post(`${url}/run`).send(req).then((res) => res.body);
},
public getFile(fileId: string): Promise<Buffer> { getFile(fileId: string): Promise<Buffer> {
return superagent.get(`${this.baseURL}/file/${fileId}`).then((res) => res.body); return superagent.get(`${url}/file/${fileId}`).then((res) => res.body);
} },
deleteFile(fileId: string): Promise<void> {
public deleteFile(fileId: string): Promise<void> { return superagent.delete(`${url}/file/${fileId}`).then((res) => res.body);
return superagent.delete(`${this.baseURL}/file/${fileId}`).then((res) => res.body); },
} listFiles(): Promise<Record<string, string>> {
return superagent.get(`${url}/file`).then((res) => res.body);
public listFiles(): Promise<Record<string, string>> { },
return superagent.get(`${this.baseURL}/file`).then((res) => res.body); version(): Promise<SandboxVersion> {
} return superagent.get(`${url}/version`).then((res) => res.body);
},
public version(): Promise<SandboxVersion> { }, {
return superagent.get(`${this.baseURL}/version`).then((res) => res.body);
}
}
const client = new Proxy({} as SandboxClient, {
get(self, key) { get(self, key) {
let url = getConfig('sandbox_host'); url = getConfig('sandbox_host');
if (url.endsWith('/')) url = url.substring(0, url.length - 1); if (url.endsWith('/')) url = url.substring(0, url.length - 1);
const c = new SandboxClient(url); return self[key];
return c[key].bind(c);
}, },
}); });