core: implement postRestart

This commit is contained in:
undefined committed 2022-03-24 20:04:08 +08:00
1 parent adce12e212
commit 040205045f
3 files changed
+14 -6

No files matched your search

+5 -4
View File
@@ -40,8 +40,9 @@ if (argv._[0] === 'backup') {
const url = buildUrl(JSON.parse(dbConfig)); const url = buildUrl(JSON.parse(dbConfig));
const dir = `${os.tmpdir()}/${Math.random().toString(36).substring(2)}`; const dir = `${os.tmpdir()}/${Math.random().toString(36).substring(2)}`;
exec('mongodump', [url, `--out=${dir}/dump`], { stdio: 'inherit' }); exec('mongodump', [url, `--out=${dir}/dump`], { stdio: 'inherit' });
const env = `${os.homedir()}/.hydro/env`; // Do not backup .env & config.json as they are all related to local installation
if (fs.existsSync(env)) fs.copySync(env, `${dir}/env`); const env = `${os.homedir()}/.hydro/addon.json`;
if (fs.existsSync(env)) fs.copySync(env, `${dir}/addon.json`);
const target = `${process.cwd()}/backup-${new Date().toISOString().replace(':', '-').split(':')[0]}.zip`; const target = `${process.cwd()}/backup-${new Date().toISOString().replace(':', '-').split(':')[0]}.zip`;
exec('zip', ['-r', target, 'dump'], { cwd: dir, stdio: 'inherit' }); exec('zip', ['-r', target, 'dump'], { cwd: dir, stdio: 'inherit' });
if (!argv.dbOnly) { if (!argv.dbOnly) {
@@ -66,8 +67,8 @@ if (argv._[0] === 'restore') {
exec('rm', ['-rf', '/data/file/*'], { stdio: 'inherit' }); exec('rm', ['-rf', '/data/file/*'], { stdio: 'inherit' });
exec('bash', ['-c', `mv ${dir}/file/* /data/file`], { stdio: 'inherit' }); exec('bash', ['-c', `mv ${dir}/file/* /data/file`], { stdio: 'inherit' });
} }
if (fs.existsSync(`${dir}/env`)) { if (fs.existsSync(`${dir}/addon.json`)) {
fs.copySync(`${dir}/env`, `${os.homedir()}/.hydro/env`, { overwrite: true }); fs.copySync(`${dir}/addon.json`, `${os.homedir()}/.hydro/addon.json`, { overwrite: true });
} }
fs.removeSync(dir); fs.removeSync(dir);
console.log('Successfully restored.'); console.log('Successfully restored.');
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "3.10.5", "version": "3.10.6",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "src/loader", "main": "src/loader",
"module": "src/loader", "module": "src/loader",
+8 -1
View File
@@ -1,8 +1,9 @@
import { exec } from 'child_process';
import { inspect } from 'util'; import { inspect } from 'util';
import Ajv from 'ajv'; import Ajv from 'ajv';
import * as yaml from 'js-yaml'; import * as yaml from 'js-yaml';
import * as check from '../check'; import * as check from '../check';
import { ValidationError } from '../error'; import { BadRequestError, ValidationError } from '../error';
import { import {
isEmail, isPassword, isUname, validate, isEmail, isPassword, isUname, validate,
} from '../lib/validator'; } from '../lib/validator';
@@ -86,6 +87,12 @@ class SystemDashboardHandler extends SystemHandler {
async get() { async get() {
this.response.template = 'manage_dashboard.html'; this.response.template = 'manage_dashboard.html';
} }
async postRestart() {
if (!process.env.pm_cwd) throw new BadRequestError('Not launched by pm2');
exec(`pm2 reload "${process.env.name}"`);
this.back();
}
} }
class SystemScriptHandler extends SystemHandler { class SystemScriptHandler extends SystemHandler {