mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
judge: update config loader
This commit is contained in:
1 parent
e9787f2ba5
commit
1c6cdfc6ff
3 files changed
+46
-58
No files matched your search
@@ -12,8 +12,8 @@
|
||||
"fs-extra": "^10.1.0",
|
||||
"js-yaml": "^4.1.0",
|
||||
"lodash": "^4.17.21",
|
||||
"mongodb": "^3.7.3",
|
||||
"p-queue": "^7.3.0",
|
||||
"schemastery": "^3.5.3",
|
||||
"shell-quote": "^1.7.3",
|
||||
"ws": "^8.8.1"
|
||||
},
|
||||
|
||||
@@ -3,63 +3,52 @@ import path from 'path';
|
||||
import cac from 'cac';
|
||||
import fs from 'fs-extra';
|
||||
import yaml from 'js-yaml';
|
||||
import Schema from 'schemastery';
|
||||
|
||||
const argv = cac().parse();
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
let CONFIG_FILE = path.resolve(os.homedir(), '.config', 'hydro', 'judge.yaml');
|
||||
|
||||
const config = {
|
||||
cache_dir: path.resolve(os.homedir(), '.cache', 'hydro', 'judge'),
|
||||
tmp_dir: path.resolve(os.tmpdir(), 'hydro', 'judge'),
|
||||
tmpfs_size: '256m',
|
||||
stdio_size: '32m',
|
||||
memoryMax: '512m',
|
||||
strict_memory: false,
|
||||
sandbox_host: 'http://localhost:5050',
|
||||
testcases_max: 100,
|
||||
total_time_limit: 60,
|
||||
processLimit: 32,
|
||||
parallelism: 2,
|
||||
singleTaskParallelism: 2,
|
||||
rerun: 0,
|
||||
rate: 1,
|
||||
config: null,
|
||||
langs: null,
|
||||
env: `\
|
||||
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
HOME=/w`,
|
||||
hosts: {},
|
||||
secret: String.random(32),
|
||||
};
|
||||
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),
|
||||
sandbox_host: Schema.string().role('link').default('http://localhost:5050'),
|
||||
testcases_max: Schema.number().default(100),
|
||||
total_time_limit: Schema.number().default(60),
|
||||
processLimit: Schema.number().default(32),
|
||||
parallelism: Schema.number().default(2),
|
||||
singleTaskParallelism: Schema.number().default(2),
|
||||
rerun: Schema.number().default(0),
|
||||
rate: Schema.number().default(1),
|
||||
env: Schema.string().default('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nHOME=/w'),
|
||||
host: Schema.any(),
|
||||
secret: Schema.string().default(String.random(32)),
|
||||
});
|
||||
|
||||
export function getConfig(key: keyof typeof config) {
|
||||
return global.Hydro ? global.Hydro.model.system.get(`hydrojudge.${key}`) ?? config[key] : config[key];
|
||||
}
|
||||
const config = global.Hydro
|
||||
? JudgeSettings({})
|
||||
: (() => {
|
||||
const cfg = JudgeSettings({});
|
||||
const configFilePath = (process.env.CONFIG_FILE || argv.options.config)
|
||||
? path.resolve(process.env.CONFIG_FILE || argv.options.config)
|
||||
: path.resolve(os.homedir(), '.config', 'hydro', 'judge.yaml');
|
||||
|
||||
if (fs.existsSync(path.resolve(process.cwd(), '.env'))) {
|
||||
const env = {};
|
||||
const f = fs.readFileSync('.env').toString();
|
||||
for (const line of f) {
|
||||
const a = line.split('=');
|
||||
env[a[0]] = a[1];
|
||||
}
|
||||
Object.assign(process.env, env);
|
||||
}
|
||||
if (process.env.TEMP_DIR || argv.options.tmp) {
|
||||
cfg.tmp_dir = path.resolve(process.env.TEMP_DIR || argv.options.tmp);
|
||||
}
|
||||
if (process.env.CACHE_DIR || argv.options.cache) {
|
||||
cfg.cache_dir = path.resolve(process.env.CACHE_DIR || argv.options.cache);
|
||||
}
|
||||
if (process.env.EXECUTION_HOST || argv.options.sandbox) {
|
||||
cfg.sandbox_host = path.resolve(process.env.EXECUTION_HOST || argv.options.sandbox);
|
||||
}
|
||||
const configFile = fs.readFileSync(configFilePath, 'utf-8');
|
||||
Object.assign(cfg, yaml.load(configFile) as any);
|
||||
return JudgeSettings(cfg);
|
||||
})();
|
||||
|
||||
if (!global.Hydro) {
|
||||
// standalone
|
||||
if (process.env.CONFIG_FILE || argv.options.config) {
|
||||
CONFIG_FILE = path.resolve(process.env.CONFIG_FILE || argv.options.config);
|
||||
}
|
||||
if (process.env.TEMP_DIR || argv.options.tmp) {
|
||||
config.tmp_dir = path.resolve(process.env.TEMP_DIR || argv.options.tmp);
|
||||
}
|
||||
if (process.env.CACHE_DIR || argv.options.cache) {
|
||||
config.cache_dir = path.resolve(process.env.CACHE_DIR || argv.options.cache);
|
||||
}
|
||||
if (process.env.EXECUTION_HOST || argv.options.sandbox) {
|
||||
config.sandbox_host = path.resolve(process.env.EXECUTION_HOST || argv.options.sandbox);
|
||||
}
|
||||
const configFile = fs.readFileSync(CONFIG_FILE).toString();
|
||||
Object.assign(config, yaml.load(configFile) as any);
|
||||
}
|
||||
export const getConfig: <K extends keyof typeof config>(key: K) => typeof config[K] = global.Hydro
|
||||
? (key) => global.Hydro.model.system.get(`hydrojudge.${key}`) ?? config[key]
|
||||
: (key) => config[key];
|
||||
@@ -2,7 +2,6 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { ObjectID } from 'mongodb';
|
||||
import type { JudgeResultBody } from 'hydrooj';
|
||||
import { end, next } from 'hydrooj/src/handler/judge';
|
||||
import { Logger } from 'hydrooj/src/logger';
|
||||
@@ -28,7 +27,7 @@ const session = {
|
||||
getNext(t: Context) {
|
||||
return (data: Partial<JudgeResultBody>) => {
|
||||
logger.debug('Next: %d %o', data);
|
||||
data.rid = new ObjectID(t.rid);
|
||||
data.rid = t.rid as any;
|
||||
if (data.case) data.case.message ||= '';
|
||||
next(data);
|
||||
};
|
||||
@@ -36,7 +35,7 @@ const session = {
|
||||
getEnd(t: Context) {
|
||||
return (data: Partial<JudgeResultBody>) => {
|
||||
data.key = 'end';
|
||||
data.rid = new ObjectID(t.rid);
|
||||
data.rid = t.rid as any;
|
||||
logger.info('End: status=%d score=%d time=%dms memory=%dkb', data.status, data.score, data.time, data.memory);
|
||||
end(data);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user