mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
fix addon load
This commit is contained in:
1 parent
2e4c818c37
commit
92335d8529
9 files changed
+80
-38
No files matched your search
+5
-22
@@ -1,10 +1,9 @@
|
|||||||
/* eslint-disable no-eval */
|
|
||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
/* eslint-disable import/no-dynamic-require */
|
/* eslint-disable import/no-dynamic-require */
|
||||||
import { argv } from 'yargs';
|
import { argv } from 'yargs';
|
||||||
import {
|
import {
|
||||||
lib, service, model, script,
|
lib, service, model,
|
||||||
builtinLib, builtinScript, builtinModel,
|
builtinLib, builtinModel,
|
||||||
} from './common';
|
} from './common';
|
||||||
|
|
||||||
const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
const COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
||||||
@@ -45,10 +44,9 @@ async function cli() {
|
|||||||
return console.log(result);
|
return console.log(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function load(call, args) {
|
export async function load() {
|
||||||
let pending = args;
|
const pending = global.addons;
|
||||||
const fail = [];
|
const fail = [];
|
||||||
const active = [];
|
|
||||||
require('../lib/i18n');
|
require('../lib/i18n');
|
||||||
require('../utils');
|
require('../utils');
|
||||||
require('../error');
|
require('../error');
|
||||||
@@ -68,21 +66,6 @@ export async function load(call, args) {
|
|||||||
await service(pending, fail);
|
await service(pending, fail);
|
||||||
for (const i of builtinModel) require(`../model/${i}`);
|
for (const i of builtinModel) require(`../model/${i}`);
|
||||||
await model(pending, fail);
|
await model(pending, fail);
|
||||||
for (const i in global.Hydro.service) {
|
for (const postInit of global.Hydro.postInit) await postInit();
|
||||||
if (global.Hydro.service[i].postInit) {
|
|
||||||
try {
|
|
||||||
await global.Hydro.service[i].postInit();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const i of builtinScript) require(`../script/${i}`);
|
|
||||||
await script(pending, fail, active);
|
|
||||||
for (const postInit of global.Hydro.postInit) {
|
|
||||||
await postInit();
|
|
||||||
}
|
|
||||||
pending = [];
|
|
||||||
await cli();
|
await cli();
|
||||||
return { active, fail };
|
|
||||||
}
|
}
|
||||||
+23
-1
@@ -1,8 +1,30 @@
|
|||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
/* eslint-disable import/no-dynamic-require */
|
/* eslint-disable import/no-dynamic-require */
|
||||||
|
import os from 'os';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs-extra';
|
||||||
|
import { argv } from 'yargs';
|
||||||
import { builtinModel } from './common';
|
import { builtinModel } from './common';
|
||||||
|
import { Entry } from '../loader';
|
||||||
|
|
||||||
export async function load(call) {
|
const tmpdir = path.resolve(os.tmpdir(), 'hydro');
|
||||||
|
const lockfile = path.resolve(tmpdir, 'lock.json');
|
||||||
|
|
||||||
|
export async function load(call: Entry) {
|
||||||
|
if (fs.existsSync(lockfile) && !argv.ignorelock) {
|
||||||
|
const file = require(lockfile);
|
||||||
|
console.error(`Lockfile exists, pid=${file.pid}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const context = {
|
||||||
|
addons: global.addons,
|
||||||
|
pid: process.pid,
|
||||||
|
ppid: process.ppid,
|
||||||
|
};
|
||||||
|
await fs.writeFile(lockfile, JSON.stringify(context));
|
||||||
|
global.onDestory.push(() => {
|
||||||
|
fs.emptyDirSync(tmpdir);
|
||||||
|
});
|
||||||
require('../lib/i18n');
|
require('../lib/i18n');
|
||||||
require('../utils');
|
require('../utils');
|
||||||
require('../error');
|
require('../error');
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import {
|
|||||||
builtinLib, builtinScript, builtinHandler, builtinModel,
|
builtinLib, builtinScript, builtinHandler, builtinModel,
|
||||||
} from './common';
|
} from './common';
|
||||||
|
|
||||||
export async function load(call, args) {
|
export async function load() {
|
||||||
let pending = args;
|
let pending = global.addons;
|
||||||
const fail = [];
|
const fail = [];
|
||||||
const active = [];
|
const active = [];
|
||||||
require('../lib/i18n');
|
require('../lib/i18n');
|
||||||
|
|||||||
+37
-10
@@ -206,11 +206,25 @@ class ProblemDetailHandler extends ProblemHandler {
|
|||||||
if (!udoc.hasPerm(PERM.PERM_CREATE_PROBLEM)) {
|
if (!udoc.hasPerm(PERM.PERM_CREATE_PROBLEM)) {
|
||||||
throw new PermissionError(PERM.PERM_CREATE_PROBLEM);
|
throw new PermissionError(PERM.PERM_CREATE_PROBLEM);
|
||||||
}
|
}
|
||||||
pid = await problem.add(
|
if (!this.pdoc.data) {
|
||||||
destDomainId, this.pdoc.pid, this.pdoc.title,
|
// Copy Without Data
|
||||||
this.pdoc.content, this.user._id, this.pdoc.tag,
|
pid = await problem.add(
|
||||||
this.pdoc.category, { domainId, pid }, hidden,
|
destDomainId, this.pdoc.pid, this.pdoc.title,
|
||||||
);
|
this.pdoc.content, this.user._id, this.pdoc.tag,
|
||||||
|
this.pdoc.category, null, hidden,
|
||||||
|
);
|
||||||
|
} else if (this.pdoc.data instanceof ObjectID) {
|
||||||
|
// With data
|
||||||
|
pid = await problem.add(
|
||||||
|
destDomainId, this.pdoc.pid, this.pdoc.title,
|
||||||
|
this.pdoc.content, this.user._id, this.pdoc.tag,
|
||||||
|
this.pdoc.category, { domainId, pid }, hidden,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// TODO better message
|
||||||
|
// Data should only be copied once.
|
||||||
|
throw new BadRequestError('Cannot copy this problem.');
|
||||||
|
}
|
||||||
this.response.redirect = this.url('problem_settings', { domainId: destDomainId, pid });
|
this.response.redirect = this.url('problem_settings', { domainId: destDomainId, pid });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -417,11 +431,24 @@ class ProblemDataDownloadHandler extends ProblemDetailHandler {
|
|||||||
if (this.pdoc.data instanceof ObjectID) {
|
if (this.pdoc.data instanceof ObjectID) {
|
||||||
this.response.redirect = await file.url(this.pdoc.data, `${this.pdoc.title}.zip`);
|
this.response.redirect = await file.url(this.pdoc.data, `${this.pdoc.title}.zip`);
|
||||||
} else if (this.pdoc.data) {
|
} else if (this.pdoc.data) {
|
||||||
// Redirect?
|
if (!this.pdoc.data.host) {
|
||||||
this.response.redirect = this.url('problem_data', {
|
this.response.redirect = this.url('problem_data', {
|
||||||
domainId: this.pdoc.domainId,
|
domainId: this.pdoc.data.domainId,
|
||||||
pid: this.pdoc.pid,
|
pid: this.pdoc.data.pid,
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
const [scheme, raw] = this.pdoc.data.host.split('//');
|
||||||
|
const args = JSON.parse(Buffer.from(raw, 'base64').toString());
|
||||||
|
if (scheme === 'hydro') {
|
||||||
|
const [secure, host, port, domainId, id] = args;
|
||||||
|
this.response.redirect = `http${secure ? 's' : ''}://${host}:${port}/d/${domainId}/p/${id}/data`;
|
||||||
|
} else if (scheme === 'syzoj') {
|
||||||
|
const [secure, host, port, id] = args;
|
||||||
|
this.response.redirect = `http${secure ? 's' : ''}://${host}:${port}/problem/${id}/testdata/download`;
|
||||||
|
} else {
|
||||||
|
throw new ProblemDataNotFoundError(pid);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else throw new ProblemDataNotFoundError(pid);
|
} else throw new ProblemDataNotFoundError(pid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-2
@@ -54,7 +54,12 @@ async function fork(args: string[] = []) {
|
|||||||
return cluster.fork();
|
return cluster.fork();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function entry(config: any) {
|
interface EntryConfig {
|
||||||
|
entry: string,
|
||||||
|
newProcess?: boolean,
|
||||||
|
}
|
||||||
|
|
||||||
|
async function entry(config: EntryConfig) {
|
||||||
if (config.entry) {
|
if (config.entry) {
|
||||||
if (config.newProcess) {
|
if (config.newProcess) {
|
||||||
const sargv = [`--entry=${config.entry}`];
|
const sargv = [`--entry=${config.entry}`];
|
||||||
@@ -74,6 +79,8 @@ async function entry(config: any) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Entry = typeof entry;
|
||||||
|
|
||||||
async function stopWorker() {
|
async function stopWorker() {
|
||||||
cluster.disconnect();
|
cluster.disconnect();
|
||||||
}
|
}
|
||||||
@@ -201,7 +208,7 @@ export async function load() {
|
|||||||
console.log(global.addons);
|
console.log(global.addons);
|
||||||
if (argv.entry) {
|
if (argv.entry) {
|
||||||
console.log(`Worker ${process.pid} Starting as ${argv.entry}`);
|
console.log(`Worker ${process.pid} Starting as ${argv.entry}`);
|
||||||
await entry({ entry: argv.entry });
|
await entry({ entry: argv.entry as string });
|
||||||
console.log(`Worker ${process.pid} Started as ${argv.entry}`);
|
console.log(`Worker ${process.pid} Started as ${argv.entry}`);
|
||||||
} else {
|
} else {
|
||||||
if (argv.firstWorker) cluster.isFirstWorker = true;
|
if (argv.firstWorker) cluster.isFirstWorker = true;
|
||||||
|
|||||||
@@ -767,6 +767,7 @@ export function Connection(
|
|||||||
sock.installHandlers(server);
|
sock.installHandlers(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO use postInit?
|
||||||
export async function start() {
|
export async function start() {
|
||||||
const [disableLog, port] = await system.getMany(['server.log', 'server.port']);
|
const [disableLog, port] = await system.getMany(['server.log', 'server.port']);
|
||||||
if (!disableLog) {
|
if (!disableLog) {
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ perm_problem: Problems
|
|||||||
perm_record: Records
|
perm_record: Records
|
||||||
perm_training: Trainings
|
perm_training: Trainings
|
||||||
problem_create: Problem Create
|
problem_create: Problem Create
|
||||||
|
problem_detail: Problem Detail
|
||||||
problem_edit: Problem Edit
|
problem_edit: Problem Edit
|
||||||
problem_import: Import Problem
|
problem_import: Import Problem
|
||||||
problem_main: Problem Set
|
problem_main: Problem Set
|
||||||
|
|||||||
@@ -523,6 +523,7 @@ Problem Solution List: 题解列表
|
|||||||
Problem Solution: 题解列表
|
Problem Solution: 题解列表
|
||||||
Problem Tags Visibility: 题目标签可见性
|
Problem Tags Visibility: 题目标签可见性
|
||||||
problem_create: 创建题目
|
problem_create: 创建题目
|
||||||
|
problem_detail: 题目详情
|
||||||
problem_edit: 编辑题目
|
problem_edit: 编辑题目
|
||||||
problem_import: 导入题目
|
problem_import: 导入题目
|
||||||
problem_main: 题库
|
problem_main: 题库
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.9.12",
|
"version": "2.9.13",
|
||||||
"main": "dist/loader.js",
|
"main": "dist/loader.js",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
|
|||||||
Reference in new issue
Block a user