mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: add ui
This commit is contained in:
1 parent
558fe34db1
commit
2cbf05dfe3
12 files changed
+168
-204
No files matched your search
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable import/first */
|
||||
/* eslint-disable no-continue */
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
/* eslint-disable no-await-in-loop */
|
||||
@@ -6,10 +7,8 @@ import './interface';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import cluster from 'cluster';
|
||||
import wtfnode from 'wtfnode';
|
||||
import fs from 'fs-extra';
|
||||
import { argv } from 'yargs';
|
||||
import AdmZip from 'adm-zip';
|
||||
import './utils';
|
||||
|
||||
export * from './interface';
|
||||
@@ -42,27 +41,13 @@ if (!global.Hydro) {
|
||||
global.addons = [];
|
||||
}
|
||||
|
||||
// eslint-disable-next-line import/first
|
||||
import { Logger } from './logger';
|
||||
// eslint-disable-next-line import/first
|
||||
import './ui';
|
||||
import * as bus from './service/bus';
|
||||
|
||||
const logger = new Logger('loader');
|
||||
wtfnode.setLogger('error', logger.error.bind(logger));
|
||||
wtfnode.setLogger('warn', logger.warn.bind(logger));
|
||||
wtfnode.setLogger('info', logger.info.bind(logger));
|
||||
logger.debug('%o', argv);
|
||||
|
||||
async function terminate() {
|
||||
if (argv.debug) wtfnode.dump();
|
||||
try {
|
||||
await bus.parallel('app/exit');
|
||||
} catch (e) {
|
||||
process.exit(1);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
async function fork(args: string[] = []) {
|
||||
const _args = process.argv.slice(2);
|
||||
_args.push(...args, `--addons=${Buffer.from(JSON.stringify(global.addons)).toString('base64')}`);
|
||||
@@ -108,16 +93,6 @@ async function startWorker(cnt: number) {
|
||||
for (let i = 1; i < cnt; i++) await fork();
|
||||
}
|
||||
|
||||
async function executeCommand(input: string) {
|
||||
try {
|
||||
const t = eval(input.toString().trim());
|
||||
if (t instanceof Promise) console.log(await t);
|
||||
else console.log(t);
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function reload(count = 1) {
|
||||
logger.info('Reloading');
|
||||
await stopWorker();
|
||||
@@ -125,30 +100,30 @@ async function reload(count = 1) {
|
||||
await startWorker(count);
|
||||
}
|
||||
|
||||
async function messageHandler(worker: cluster.Worker, msg: any) {
|
||||
if (!msg) msg = worker;
|
||||
if (msg.event) {
|
||||
if (msg.event === 'bus') {
|
||||
if (cluster.isMaster) {
|
||||
for (const i in cluster.workers) {
|
||||
cluster.workers[i].send(msg);
|
||||
}
|
||||
} else {
|
||||
global.Hydro.service.bus.emit(msg.eventName, ...msg.payload);
|
||||
}
|
||||
} else if (msg.event === 'stat') {
|
||||
global.Hydro.stat.reqCount += msg.count;
|
||||
} else if (msg.event === 'restart') {
|
||||
await reload(msg.count);
|
||||
} else if (msg.event === 'run') {
|
||||
await executeCommand(msg.command);
|
||||
const shell = new Logger('shell');
|
||||
async function executeCommand(input: string) {
|
||||
// Clear the stack
|
||||
setImmediate(async () => {
|
||||
if (input === 'restart') return reload();
|
||||
if (input === 'exit' || input === 'quit' || input === 'shutdown') {
|
||||
return process.kill(process.pid, 'SIGINT');
|
||||
}
|
||||
}
|
||||
try {
|
||||
// eslint-disable-next-line no-eval
|
||||
shell.info(await eval(input.trim()));
|
||||
} catch (e) {
|
||||
shell.warn(e);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
const moduleTemp = path.resolve(os.tmpdir(), 'hydro', 'module');
|
||||
bus.on('message/reload', reload);
|
||||
bus.on('message/run', executeCommand);
|
||||
process.on('unhandledRejection', logger.error);
|
||||
process.on('uncaughtException', logger.error);
|
||||
|
||||
const publicTemp = path.resolve(os.tmpdir(), 'hydro', 'public');
|
||||
const tmp = path.resolve(os.tmpdir(), 'hydro', '__');
|
||||
|
||||
export function addon(addonPath: string) {
|
||||
let modulePath = path.resolve(process.cwd(), addonPath);
|
||||
@@ -163,53 +138,14 @@ export function addon(addonPath: string) {
|
||||
} catch (e) {
|
||||
logger.error(`Addon not found: ${addonPath}`);
|
||||
}
|
||||
} else if (modulePath.endsWith('.hydro')) {
|
||||
try {
|
||||
// Is *.hydro module
|
||||
const t = modulePath.split(path.sep);
|
||||
const name = t[t.length - 1].split('.')[0];
|
||||
const zip = new AdmZip(modulePath);
|
||||
const targetPath = path.resolve(moduleTemp, name);
|
||||
zip.extractAllTo(targetPath, true);
|
||||
const content = fs.readdirSync(targetPath);
|
||||
const ipath = path.join(targetPath, content[0]);
|
||||
if (content.length === 1 && fs.statSync(ipath).isDirectory()) {
|
||||
fs.moveSync(ipath, tmp);
|
||||
fs.rmdirSync(targetPath);
|
||||
fs.moveSync(tmp, targetPath);
|
||||
}
|
||||
const publicPath = path.resolve(targetPath, 'public');
|
||||
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
|
||||
global.addons.push(targetPath);
|
||||
} catch (e) {
|
||||
logger.error('Addon load fail: ', e);
|
||||
throw e;
|
||||
}
|
||||
} else logger.error(`Addon not found: ${addonPath}`);
|
||||
}
|
||||
|
||||
process.on('unhandledRejection', logger.error);
|
||||
process.on('SIGINT', terminate);
|
||||
|
||||
export async function load() {
|
||||
addon(path.resolve(__dirname, '..'));
|
||||
Error.stackTraceLimit = 50;
|
||||
process.on('message', messageHandler);
|
||||
cluster.on('message', messageHandler);
|
||||
if (cluster.isMaster || argv.startAsMaster) {
|
||||
logger.info(`Master ${process.pid} Starting`);
|
||||
process.stdin.setEncoding('utf8');
|
||||
process.stdin.on('data', (buf) => {
|
||||
const input = buf.toString();
|
||||
if (input[0] === '@') {
|
||||
for (const i in cluster.workers) {
|
||||
cluster.workers[i].send({ event: 'run', command: input.substr(1, input.length - 1) });
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
executeCommand(input);
|
||||
}
|
||||
});
|
||||
const cnt = await entry({ entry: 'master' });
|
||||
logger.info('Master started');
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
@@ -245,10 +181,10 @@ export async function load() {
|
||||
|
||||
export async function loadCli() {
|
||||
await entry({ entry: 'cli' });
|
||||
return terminate();
|
||||
process.kill(process.pid, 'SIGINT');
|
||||
}
|
||||
|
||||
if (argv.pandora || !module.parent) {
|
||||
if (argv.pandora || require.main === module) {
|
||||
const func = argv._[0] === 'cli' ? load : loadCli;
|
||||
func().catch((e) => {
|
||||
logger.error(e);
|
||||
|
||||
Reference in new issue
Block a user