mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: pm2 cluster management
This commit is contained in:
1 parent
9f5c00a985
commit
448c398d21
30 files changed
+206
-427
No files matched your search
@@ -1,84 +0,0 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import cac from 'cac';
|
||||
import { builtinModel } from './common';
|
||||
import { Entry } from '../loader';
|
||||
import { Logger } from '../logger';
|
||||
import options from '../options';
|
||||
import * as bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
|
||||
const argv = cac().parse();
|
||||
const logger = new Logger('entry/master');
|
||||
const tmpdir = path.resolve(os.tmpdir(), 'hydro');
|
||||
const lockfile = path.resolve(tmpdir, 'lock.json');
|
||||
|
||||
export async function load(call: Entry) {
|
||||
fs.ensureDirSync(tmpdir);
|
||||
if (fs.existsSync(lockfile) && !argv.options.ignorelock) {
|
||||
try {
|
||||
const file = require(lockfile);
|
||||
process.kill(file.pid, 0);
|
||||
logger.error(`Lockfile exists, pid=${file.pid}`);
|
||||
process.exit(1);
|
||||
} catch {
|
||||
// Invalid lockfile or process not exist
|
||||
}
|
||||
}
|
||||
const context = {
|
||||
addons: global.addons,
|
||||
pid: process.pid,
|
||||
ppid: process.ppid,
|
||||
};
|
||||
await fs.writeFile(lockfile, JSON.stringify(context));
|
||||
require('../lib/i18n');
|
||||
require('../utils');
|
||||
require('../error');
|
||||
const config = require('../options')();
|
||||
if (!config) {
|
||||
logger.info('Starting setup');
|
||||
await call({ entry: 'setup', newProcess: true }).catch((err) => {
|
||||
logger.error('Cannot start setup process.', err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
bus.once('app/exit', () => {
|
||||
fs.removeSync(lockfile);
|
||||
fs.removeSync(path.resolve(os.tmpdir(), 'hydro', 'public'));
|
||||
});
|
||||
const opts = options();
|
||||
await db.start(opts);
|
||||
const modelSystem = require('../model/system');
|
||||
await modelSystem.runConfig();
|
||||
if (process.env.MINIO_ACCESS_KEY && process.env.MINIO_SECRET_KEY) {
|
||||
await modelSystem.set('file.accessKey', process.env.MINIO_ACCESS_KEY);
|
||||
await modelSystem.set('file.secretKey', process.env.MINIO_SECRET_KEY);
|
||||
}
|
||||
const storage = require('../service/storage');
|
||||
await storage.start();
|
||||
require('../service/monitor');
|
||||
for (const i of builtinModel) require(`../model/${i}`);
|
||||
const scripts = require('../upgrade').default;
|
||||
let dbVer = (await modelSystem.get('db.ver')) ?? 0;
|
||||
const isFresh = !dbVer;
|
||||
const expected = scripts.length;
|
||||
while (dbVer < expected) {
|
||||
logger.info('Upgrading database: from %d to %d', dbVer, expected);
|
||||
const func = scripts[dbVer];
|
||||
if (typeof func !== 'function' || (isFresh && func.toString().includes('_FRESH_INSTALL_IGNORE'))) {
|
||||
dbVer++;
|
||||
continue;
|
||||
}
|
||||
const result = await func();
|
||||
if (!result) break;
|
||||
dbVer++;
|
||||
await modelSystem.set('db.ver', dbVer);
|
||||
}
|
||||
await bus.serial('app/started');
|
||||
return typeof argv.options.worker === 'number'
|
||||
? argv.options.worker
|
||||
: await modelSystem.get('server.worker');
|
||||
}
|
||||
@@ -106,13 +106,7 @@ async function post(ctx: Context) {
|
||||
fs.writeFileSync(path.resolve(os.homedir(), '.hydro', 'config.json'), JSON.stringify({
|
||||
host, port, name, username, password,
|
||||
}));
|
||||
ctx.body = `<h1>This page will reload in 3 secs.</h1>
|
||||
<script>
|
||||
setTimeout(function (){
|
||||
window.location.href = '/';
|
||||
}, 3000);
|
||||
</script>`;
|
||||
setTimeout(() => process.exit(0), 500);
|
||||
ctx.body = '<h1>Done! Please restart Hydro.</h1>';
|
||||
} catch (e) {
|
||||
ctx.body = `Error connecting to database: ${e.message}\n${e.stack}`;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
import os from 'os';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import cac from 'cac';
|
||||
import {
|
||||
locale, template, lib, service, model, handler, script, setting, uistatic,
|
||||
@@ -12,17 +15,22 @@ import { Logger } from '../logger';
|
||||
|
||||
const logger = new Logger('loader/worker');
|
||||
const detail = cac().parse().options.loaderDetail;
|
||||
const tmpdir = path.resolve(os.tmpdir(), 'hydro');
|
||||
|
||||
export async function load() {
|
||||
fs.ensureDirSync(tmpdir);
|
||||
require('../lib/i18n');
|
||||
require('../utils');
|
||||
require('../error');
|
||||
const config = require('../options')();
|
||||
if (!config) {
|
||||
logger.info('Starting setup');
|
||||
return require('./setup').load();
|
||||
}
|
||||
const pending = global.addons;
|
||||
const fail = [];
|
||||
const active = [];
|
||||
if (detail) logger.info('start');
|
||||
require('../lib/i18n');
|
||||
require('../utils');
|
||||
require('../error');
|
||||
require('../options');
|
||||
if (detail) logger.info('finish: options');
|
||||
await Promise.all([
|
||||
locale(pending, fail),
|
||||
template(pending, fail),
|
||||
@@ -80,9 +88,23 @@ export async function load() {
|
||||
if (detail) logger.info('finish: bus.serial(start)');
|
||||
await server.start();
|
||||
if (detail) logger.info('finish: server.start');
|
||||
setInterval(() => {
|
||||
process.send({ event: 'stat', count: global.Hydro.stat.reqCount });
|
||||
global.Hydro.stat.reqCount = 0;
|
||||
}, 30 * 1000);
|
||||
if (process.env.NODE_APP_INSTANCE === '0') {
|
||||
const scripts = require('../upgrade').default;
|
||||
let dbVer = (await modelSystem.get('db.ver')) ?? 0;
|
||||
const isFresh = !dbVer;
|
||||
const expected = scripts.length;
|
||||
while (dbVer < expected) {
|
||||
const func = scripts[dbVer];
|
||||
if (typeof func !== 'function' || (isFresh && func.toString().includes('_FRESH_INSTALL_IGNORE'))) {
|
||||
dbVer++;
|
||||
continue;
|
||||
}
|
||||
logger.info('Upgrading database: from %d to %d', dbVer, expected);
|
||||
const result = await func();
|
||||
if (!result) break;
|
||||
dbVer++;
|
||||
await modelSystem.set('db.ver', dbVer);
|
||||
}
|
||||
}
|
||||
return { active, fail };
|
||||
}
|
||||
Reference in new issue
Block a user