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