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