mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: allow run script via cli
This commit is contained in:
1 parent
260261fcfe
commit
a2edf18737
6 files changed
+36
-39
No files matched your search
@@ -3,10 +3,11 @@
|
|||||||
import { argv } from 'yargs';
|
import { argv } from 'yargs';
|
||||||
import { ObjectID } from 'mongodb';
|
import { ObjectID } from 'mongodb';
|
||||||
import {
|
import {
|
||||||
lib, service, model,
|
lib, service, model, script,
|
||||||
builtinLib, builtinModel,
|
builtinLib, builtinModel, builtinScript,
|
||||||
} from './common';
|
} from './common';
|
||||||
import options from '../options';
|
import options from '../options';
|
||||||
|
import { validate } from '../lib/validator';
|
||||||
import * as bus from '../service/bus';
|
import * as bus from '../service/bus';
|
||||||
import db from '../service/db';
|
import db from '../service/db';
|
||||||
|
|
||||||
@@ -21,8 +22,23 @@ function parseParameters(fn: Function) {
|
|||||||
return result === null ? [] : result;
|
return result === null ? [] : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function runScript(name: string, arg: any) {
|
||||||
|
if (!global.Hydro.script[name]) return console.error('Script %s not found.', name);
|
||||||
|
validate(global.Hydro.script[name].validate, arg);
|
||||||
|
return await global.Hydro.script[name].run(arg, console.info);
|
||||||
|
}
|
||||||
|
|
||||||
async function cli() {
|
async function cli() {
|
||||||
const [, modelName, func, ...args] = argv._ as [string, string, string, ...any[]];
|
const [, modelName, func, ...args] = argv._ as [string, string, string, ...any[]];
|
||||||
|
if (modelName === 'script') {
|
||||||
|
let arg: any;
|
||||||
|
try {
|
||||||
|
arg = JSON.parse(args.join(' '));
|
||||||
|
} catch (e) {
|
||||||
|
return console.error('Invalid argument');
|
||||||
|
}
|
||||||
|
return await runScript(func, arg);
|
||||||
|
}
|
||||||
if (!global.Hydro.model[modelName]) {
|
if (!global.Hydro.model[modelName]) {
|
||||||
return console.error(`Model ${modelName} doesn't exist.`);
|
return console.error(`Model ${modelName} doesn't exist.`);
|
||||||
}
|
}
|
||||||
@@ -65,22 +81,18 @@ export async function load() {
|
|||||||
require('../options');
|
require('../options');
|
||||||
const opts = options();
|
const opts = options();
|
||||||
await db.start(opts);
|
await db.start(opts);
|
||||||
const modelSystem = require('../model/system');
|
|
||||||
const [endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge] = modelSystem.getMany([
|
|
||||||
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
|
|
||||||
'file.endPointForUser', 'file.endPointForJudge',
|
|
||||||
]);
|
|
||||||
const sopts = {
|
|
||||||
endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge,
|
|
||||||
};
|
|
||||||
const storage = require('../service/storage');
|
const storage = require('../service/storage');
|
||||||
await storage.start(sopts);
|
await storage.start();
|
||||||
for (const i of builtinLib) require(`../lib/${i}`);
|
for (const i of builtinLib) require(`../lib/${i}`);
|
||||||
await lib(pending, fail);
|
await lib(pending, fail);
|
||||||
|
const systemModel = require('../model/system');
|
||||||
|
await systemModel.runConfig();
|
||||||
require('../service/gridfs');
|
require('../service/gridfs');
|
||||||
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 of builtinScript) require(`../script/${i}`);
|
||||||
|
await script(pending, fail, []);
|
||||||
await bus.parallel('app/started');
|
await bus.parallel('app/started');
|
||||||
await cli();
|
await cli();
|
||||||
}
|
}
|
||||||
@@ -57,15 +57,8 @@ export async function load(call: Entry) {
|
|||||||
await modelSystem.set('file.secretKey', process.env.MINIO_SECRET_KEY);
|
await modelSystem.set('file.secretKey', process.env.MINIO_SECRET_KEY);
|
||||||
await modelSystem.set('file.endPoint', 'http://localhost:9000/');
|
await modelSystem.set('file.endPoint', 'http://localhost:9000/');
|
||||||
}
|
}
|
||||||
const [endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge] = modelSystem.getMany([
|
|
||||||
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
|
|
||||||
'file.endPointForUser', 'file.endPointForJudge',
|
|
||||||
]);
|
|
||||||
const sopts = {
|
|
||||||
endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge,
|
|
||||||
};
|
|
||||||
const storage = require('../service/storage');
|
const storage = require('../service/storage');
|
||||||
await storage.start(sopts);
|
await storage.start();
|
||||||
require('../service/monitor');
|
require('../service/monitor');
|
||||||
for (const i of builtinModel) require(`../model/${i}`);
|
for (const i of builtinModel) require(`../model/${i}`);
|
||||||
const scripts = require('../upgrade').default;
|
const scripts = require('../upgrade').default;
|
||||||
|
|||||||
@@ -34,15 +34,8 @@ export async function load() {
|
|||||||
const modelSystem = require('../model/system');
|
const modelSystem = require('../model/system');
|
||||||
await modelSystem.runConfig();
|
await modelSystem.runConfig();
|
||||||
if (argv.loaderDetail) logger.info('finish: config');
|
if (argv.loaderDetail) logger.info('finish: config');
|
||||||
const [endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge] = modelSystem.getMany([
|
|
||||||
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
|
|
||||||
'file.endPointForUser', 'file.endPointForJudge',
|
|
||||||
]);
|
|
||||||
const sopts = {
|
|
||||||
endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge,
|
|
||||||
};
|
|
||||||
const storage = require('../service/storage');
|
const storage = require('../service/storage');
|
||||||
storage.start(sopts);
|
await storage.start();
|
||||||
if (argv.loaderDetail) logger.info('finish: storage.connect');
|
if (argv.loaderDetail) logger.info('finish: storage.connect');
|
||||||
for (const i of builtinLib) {
|
for (const i of builtinLib) {
|
||||||
let t;
|
let t;
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ export async function runConfig() {
|
|||||||
}
|
}
|
||||||
const config = await coll.find().toArray();
|
const config = await coll.find().toArray();
|
||||||
for (const i of config) cache[i._id] = i.value;
|
for (const i of config) cache[i._id] = i.value;
|
||||||
bus.emit('database/config');
|
await bus.emit('database/config');
|
||||||
}
|
}
|
||||||
|
|
||||||
bus.on('system/setting', (args) => {
|
bus.on('system/setting', (args) => {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export interface EventMap extends Record<string, any> {
|
|||||||
'message/run': (command: string) => VoidReturn
|
'message/run': (command: string) => VoidReturn
|
||||||
|
|
||||||
'database/connect': (db: Db) => void
|
'database/connect': (db: Db) => void
|
||||||
'database/config': () => void
|
'database/config': () => VoidReturn
|
||||||
|
|
||||||
'system/setting': (args: Record<string, any>) => VoidReturn
|
'system/setting': (args: Record<string, any>) => VoidReturn
|
||||||
|
|
||||||
|
|||||||
@@ -79,9 +79,15 @@ class StorageService {
|
|||||||
private opts: StorageOptions;
|
private opts: StorageOptions;
|
||||||
private replaceWithAlternativeUrlFor: Record<'user' | 'judge', (originalUrl: string) => string>;
|
private replaceWithAlternativeUrlFor: Record<'user' | 'judge', (originalUrl: string) => string>;
|
||||||
|
|
||||||
async start(opts: StorageOptions) {
|
async start() {
|
||||||
try {
|
try {
|
||||||
this.opts = opts;
|
const [endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge] = system.getMany([
|
||||||
|
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
|
||||||
|
'file.endPointForUser', 'file.endPointForJudge',
|
||||||
|
]);
|
||||||
|
this.opts = {
|
||||||
|
endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge,
|
||||||
|
};
|
||||||
this.client = new Client({
|
this.client = new Client({
|
||||||
...parseMainEndpointUrl(this.opts.endPoint),
|
...parseMainEndpointUrl(this.opts.endPoint),
|
||||||
accessKey: this.opts.accessKey,
|
accessKey: this.opts.accessKey,
|
||||||
@@ -103,14 +109,7 @@ class StorageService {
|
|||||||
logger.warn('Storage init fail. will retry later.');
|
logger.warn('Storage init fail. will retry later.');
|
||||||
this.error = e.toString();
|
this.error = e.toString();
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
const [endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge] = system.getMany([
|
await this.start();
|
||||||
'file.endPoint', 'file.accessKey', 'file.secretKey', 'file.bucket', 'file.region',
|
|
||||||
'file.endPointForUser', 'file.endPointForJudge',
|
|
||||||
]);
|
|
||||||
const sopts = {
|
|
||||||
endPoint, accessKey, secretKey, bucket, region, endPointForUser, endPointForJudge,
|
|
||||||
};
|
|
||||||
await this.start(sopts);
|
|
||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user