diff --git a/packages/hydrojudge/locales/zh.yaml b/packages/hydrojudge/locales/zh.yaml index 141bc8627..7ad955502 100644 --- a/packages/hydrojudge/locales/zh.yaml +++ b/packages/hydrojudge/locales/zh.yaml @@ -27,7 +27,6 @@ Syncing testdata, please wait...: 正在同步测试数据,请稍候... Temp directory: 临时目录 Testdata cache directory: 测试数据缓存目录 Testdata configuration incorrect.: 题目配置错误。 -Tmpfs size: tmpfs 分区大小 Too many files.: 文件过多。 Too many testcases. Cancelled.: 测试数据过多。评测取消。 Total time limit longer than {0}s. Cancelled.: 总时限超过{0}s。评测取消。 diff --git a/packages/hydrojudge/setting.yaml b/packages/hydrojudge/setting.yaml index 1ed0fbe98..f4e4c5a63 100644 --- a/packages/hydrojudge/setting.yaml +++ b/packages/hydrojudge/setting.yaml @@ -8,11 +8,6 @@ tmp_dir: name: tmp_dir desc: Temp directory default: $TEMP/hydro/judge -tmpfs_size: - type: text - name: tmpfs_size - desc: Tmpfs size - default: 256m stdio_size: type: text name: stdio_size diff --git a/packages/hydrojudge/src/config.ts b/packages/hydrojudge/src/config.ts index 7f9dfe37c..25eb82065 100644 --- a/packages/hydrojudge/src/config.ts +++ b/packages/hydrojudge/src/config.ts @@ -10,7 +10,6 @@ const argv = cac().parse(); const JudgeSettings = Schema.object({ cache_dir: Schema.string().default(path.resolve(os.homedir(), '.cache', 'hydro', 'judge')), tmp_dir: Schema.string().default(path.resolve(os.tmpdir(), 'hydro', 'judge')), - tmpfs_size: Schema.string().pattern(/^\d+[kmg]b?$/g).default('256m'), stdio_size: Schema.string().pattern(/^\d+[kmg]b?$/g).default('32m'), memoryMax: Schema.string().pattern(/^\d+[kmg]b?$/g).default('512m'), strict_memory: Schema.boolean().default(false), diff --git a/packages/hydrojudge/src/daemon.ts b/packages/hydrojudge/src/daemon.ts index 26396b24a..68c5af0b3 100644 --- a/packages/hydrojudge/src/daemon.ts +++ b/packages/hydrojudge/src/daemon.ts @@ -35,7 +35,7 @@ let exit = false; const terminate = async () => { log.info('正在保存数据'); try { - await Promise.all(global.onDestroy.map((f: Function) => f())); + await Promise.all(global.hosts.map((f) => f.dispose?.())); process.exit(1); } catch (e) { if (exit) process.exit(1); diff --git a/packages/hydrojudge/src/hosts/hydro.ts b/packages/hydrojudge/src/hosts/hydro.ts index 6cc78e4e8..0495f118f 100644 --- a/packages/hydrojudge/src/hosts/hydro.ts +++ b/packages/hydrojudge/src/hosts/hydro.ts @@ -173,7 +173,6 @@ export default class Hydro { Authorization: `Bearer ${this.config.cookie.split('sid=')[1].split(';')[0]}`, }, }); - global.onDestroy.push(() => this.ws.close()); const content = this.config.minPriority !== undefined ? `{"key":"prio","prio":${this.config.minPriority}}` : '{"key":"ping"}'; @@ -207,6 +206,10 @@ export default class Hydro { log.info(`[${this.config.host}] 已连接`); } + dispose() { + this.ws?.close?.(); + } + async setCookie(cookie: string) { this.config.cookie = cookie; } diff --git a/packages/hydrojudge/src/judge/hack.ts b/packages/hydrojudge/src/judge/hack.ts index 9df9a91f8..3f02400a8 100644 --- a/packages/hydrojudge/src/judge/hack.ts +++ b/packages/hydrojudge/src/judge/hack.ts @@ -1,5 +1,6 @@ /* eslint-disable no-sequences */ import { basename } from 'path'; +import { unlinkSync } from 'fs'; import { STATUS } from '@hydrooj/utils/lib/status'; import checkers from '../checkers'; import compile, { compileChecker, compileValidator } from '../compile'; @@ -25,6 +26,7 @@ export async function judge(ctx: Context) { compileValidator(session.getLang, config.validator, judgeExtraFiles).then(markCleanup), ctx.session.fetchFile(ctx.files.hack), ]); + ctx.clean.push(async () => unlinkSync(input)); ctx.next({ status: STATUS.STATUS_JUDGING, progress: 0 }); const validateResult = await run( validator.execute, diff --git a/packages/hydrojudge/src/service.ts b/packages/hydrojudge/src/service.ts index bb64dd417..40b23b27f 100644 --- a/packages/hydrojudge/src/service.ts +++ b/packages/hydrojudge/src/service.ts @@ -4,7 +4,6 @@ declare module 'hydrooj' { interface SystemKeys { 'hydrojudge.cache_dir': string; 'hydrojudge.tmp_dir': string; - 'hydrojudge.tmpfs_size': string; 'hydrojudge.sandbox_host': string; 'hydrojudge.memoryMax': string; 'hydrojudge.testcases_max': number; diff --git a/packages/hydrojudge/src/task.ts b/packages/hydrojudge/src/task.ts index 6b59526da..4f76be2dd 100644 --- a/packages/hydrojudge/src/task.ts +++ b/packages/hydrojudge/src/task.ts @@ -12,8 +12,7 @@ import { NextFunction, ParsedConfig } from './interface'; import judge from './judge'; import { Logger } from './log'; import { CopyInFile } from './sandbox'; -import * as tmpfs from './tmpfs'; -import { compilerText, Lock, md5 } from './utils'; +import { compilerText, md5 } from './utils'; interface Session { getLang: (name: string) => LangConfig; @@ -32,7 +31,6 @@ export class JudgeTask { rid: string; lang: string; code: CopyInFile; - tmpdir: string; input?: string; clean: (() => Promise)[]; data: FileInfo[]; @@ -73,11 +71,7 @@ export class JudgeTask { }; this.next = this.session.getNext(this); this.end = this.session.getEnd(this); - this.tmpdir = path.resolve(getConfig('tmp_dir'), this.rid); this.clean = []; - await Lock.acquire(`${host}/${this.source}/${this.rid}`); - fs.ensureDirSync(this.tmpdir); - tmpfs.mount(this.tmpdir, getConfig('tmpfs_size')); logger.info('Submission: %s/%s/%s', host, this.source, this.rid); await this.doSubmission(); } catch (e) { @@ -100,11 +94,8 @@ export class JudgeTask { }); } } finally { - Lock.release(`${host}/${this.source}/${this.rid}`); // eslint-disable-next-line no-await-in-loop for (const clean of this.clean) await clean()?.catch(() => null); - tmpfs.umount(this.tmpdir); - fs.removeSync(this.tmpdir); } } diff --git a/packages/hydrojudge/src/tmpfs.ts b/packages/hydrojudge/src/tmpfs.ts deleted file mode 100644 index 2fe21cb7c..000000000 --- a/packages/hydrojudge/src/tmpfs.ts +++ /dev/null @@ -1,22 +0,0 @@ -import assert from 'assert'; -import child from 'child_process'; -import os from 'os'; -import fs from 'fs-extra'; -import log from './log'; - -const linux = os.platform() === 'linux'; -if (!linux) log.warn('Not running on linux. tmpfs disabled.'); - -const userInfo = os.userInfo(); -const uid = userInfo.uid; -if (uid !== 0) log.warn('Not running by root. tmpfs disabled.'); - -export function mount(path: string, size = '32m') { - fs.ensureDirSync(path); - assert(size.match(/^\d+[kmg]b?$/gi)); - if (linux && uid === 0) child.execSync(`mount tmpfs ${path} -t tmpfs -o size=${size}`); -} - -export function umount(path: string) { - if (linux && uid === 0) child.execSync(`umount ${path}`); -}