diff --git a/back/loaders/server.ts b/back/loaders/server.ts index b060299b..dd010777 100644 --- a/back/loaders/server.ts +++ b/back/loaders/server.ts @@ -5,28 +5,6 @@ import Sock from './sock'; export default async ({ server }: { server: Server }) => { await Sock({ server }); Logger.info('✌️ Sock loaded'); - let exitTime = 0; - let timer: NodeJS.Timeout; - - process.on('SIGINT', (singal) => { - Logger.warn(`Server need close, singal ${singal}`); - console.warn(`Server need close, singal ${singal}`); - exitTime++; - if (exitTime >= 3) { - Logger.warn('Forcing server close'); - console.warn('Forcing server close'); - clearTimeout(timer); - process.exit(1); - } - server.close(() => { - if (timer) { - clearTimeout(timer); - } - timer = setTimeout(() => { - process.exit(); - }, 15000); - }); - }); process.on('uncaughtException', (error) => { Logger.error('Uncaught exception:', error); diff --git a/back/loaders/update.ts b/back/loaders/update.ts index 84987b93..3a6dd8ac 100644 --- a/back/loaders/update.ts +++ b/back/loaders/update.ts @@ -4,9 +4,9 @@ import cors from 'cors'; import { Application, NextFunction, Request, Response } from 'express'; import jwt from 'express-jwt'; import Container from 'typedi'; -import Logger from './logger'; import config from '../config'; import SystemService from '../services/system'; +import Logger from './logger'; export default ({ app }: { app: Application }) => { app.set('trust proxy', 'loopback'); @@ -22,6 +22,20 @@ export default ({ app }: { app: Application }) => { }), ); + app.put( + '/api/reload', + async (req: Request, res: Response, next: NextFunction) => { + try { + const systemService = Container.get(SystemService); + const result = await systemService.reloadSystem(); + res.send(result); + } catch (e) { + Logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); + app.put( '/api/system', async (req: Request, res: Response, next: NextFunction) => { @@ -35,6 +49,7 @@ export default ({ app }: { app: Application }) => { } }, ); + app.put( '/api/data', async (req: Request, res: Response, next: NextFunction) => { diff --git a/back/services/system.ts b/back/services/system.ts index d1599f03..074052d7 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -1,20 +1,14 @@ +import { spawn } from 'cross-spawn'; import { Response } from 'express'; -import { Service, Inject } from 'typedi'; +import fs from 'fs'; +import got from 'got'; +import sum from 'lodash/sum'; +import path from 'path'; +import tar from 'tar'; +import { Inject, Service } from 'typedi'; import winston from 'winston'; import config from '../config'; -import { - AuthDataType, - AuthInfo, - SystemInstance, - SystemModel, - SystemModelInfo, -} from '../data/system'; -import { NotificationInfo } from '../data/notify'; -import NotificationService from './notify'; -import ScheduleService, { TaskCallbacks } from './schedule'; -import { spawn } from 'cross-spawn'; -import SockService from './sock'; -import got from 'got'; +import { TASK_COMMAND } from '../config/const'; import { getPid, killTask, @@ -23,13 +17,23 @@ import { promiseExec, readDirs, } from '../config/util'; -import { TASK_COMMAND } from '../config/const'; +import { + DependenceModel, + DependenceStatus, + DependenceTypes, +} from '../data/dependence'; +import { NotificationInfo } from '../data/notify'; +import { + AuthDataType, + AuthInfo, + SystemInstance, + SystemModel, + SystemModelInfo, +} from '../data/system'; import taskLimit from '../shared/pLimit'; -import tar from 'tar'; -import path from 'path'; -import fs from 'fs'; -import sum from 'lodash/sum'; -import { DependenceModel, DependenceStatus, DependenceTypes } from '../data/dependence'; +import NotificationService from './notify'; +import ScheduleService, { TaskCallbacks } from './schedule'; +import SockService from './sock'; @Service() export default class SystemService { @@ -139,7 +143,10 @@ export default class SystemService { } let command = `cd && ${cmd}`; const docs = await DependenceModel.findAll({ - where: { type: DependenceTypes.nodejs, status: DependenceStatus.installed }, + where: { + type: DependenceTypes.nodejs, + status: DependenceStatus.installed, + }, }); if (docs.length > 0) { command += ` && pnpm i -g`; @@ -326,7 +333,7 @@ export default class SystemService { return { code: 200 }; } - public async reloadSystem(target: 'system' | 'data') { + public async reloadSystem(target?: 'system' | 'data') { const cmd = `real_time=true ql reload ${target || ''}`; const cp = spawn(cmd, { shell: '/bin/bash' }); cp.unref(); @@ -382,8 +389,13 @@ export default class SystemService { public async exportData(res: Response) { try { - await tar.create( - { gzip: true, file: config.dataTgzFile, cwd: config.rootPath }, + tar.create( + { + gzip: true, + file: config.dataTgzFile, + cwd: config.rootPath, + sync: true, + }, ['data'], ); res.download(config.dataTgzFile); @@ -395,7 +407,7 @@ export default class SystemService { public async importData() { try { await promiseExec(`rm -rf ${path.join(config.tmpPath, 'data')}`); - await tar.x({ file: config.dataTgzFile, cwd: config.tmpPath }); + tar.x({ file: config.dataTgzFile, cwd: config.tmpPath, sync: true }); return { code: 200 }; } catch (error: any) { return { code: 400, message: error.message }; diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 6f52ddd7..b2c108ed 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -21,6 +21,7 @@ nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf echo -e "nginx启动成功...\n" echo -e "======================4. 启动pm2服务========================\n" +reload_update reload_pm2 if [[ $AutoStartBot == true ]]; then diff --git a/ecosystem.config.js b/ecosystem.config.js index 0bf356ca..0a48440c 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -1,14 +1,5 @@ module.exports = { apps: [ - { - name: 'update', - max_restarts: 10, - kill_timeout: 15000, - wait_ready: true, - listen_timeout: 10000, - time: true, - script: 'static/build/update.js', - }, { name: 'schedule', max_restarts: 10, diff --git a/other.config.js b/other.config.js new file mode 100644 index 00000000..d8c1852b --- /dev/null +++ b/other.config.js @@ -0,0 +1,13 @@ +module.exports = { + apps: [ + { + name: 'update', + max_restarts: 10, + kill_timeout: 15000, + wait_ready: true, + listen_timeout: 10000, + time: true, + script: 'static/build/update.js', + }, + ], +}; diff --git a/shell/check.sh b/shell/check.sh index 4486279b..3df4c113 100755 --- a/shell/check.sh +++ b/shell/check.sh @@ -81,6 +81,7 @@ main() { check_ql check_nginx check_pm2 + reload_update reload_pm2 echo -e "\n=====> 检测结束\n" } diff --git a/shell/share.sh b/shell/share.sh index 403804a1..a6405994 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -305,6 +305,11 @@ random_range() { echo $((RANDOM % ($end - $beg) + $beg)) } +delete_pm2() { + cd $dir_root + pm2 delete ecosystem.config.js +} + reload_pm2() { cd $dir_root restore_env_vars @@ -312,6 +317,13 @@ reload_pm2() { pm2 startOrGracefulReload ecosystem.config.js } +reload_update() { + cd $dir_root + restore_env_vars + pm2 flush &>/dev/null + pm2 startOrGracefulReload other.config.js +} + diff_time() { local format="$1" local begin_time="$2" diff --git a/shell/update.sh b/shell/update.sh index c26e9b95..d04b8d04 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -231,6 +231,8 @@ usage() { } reload_qinglong() { + delete_pm2 + local reload_target="${1}" local primary_branch="master" if [[ "${QL_BRANCH}" == "develop" ]]; then @@ -245,8 +247,8 @@ reload_qinglong() { fi if [[ "$reload_target" == 'data' ]]; then - rm -rf ${dir_root}/data - cp -rf ${dir_tmp}/data ${dir_root}/ + rm -rf ${dir_root}/data/* + mv -f ${dir_tmp}/data/* ${dir_root}/data/ fi reload_pm2 @@ -310,6 +312,8 @@ check_update_dep() { echo -e "更新包下载成功..." if [[ "$needRestart" == 'true' ]]; then + delete_pm2 + cp -rf ${dir_tmp}/qinglong-${primary_branch}/* ${dir_root}/ rm -rf $dir_static/* cp -rf ${dir_tmp}/qinglong-static-${primary_branch}/* ${dir_static}/ diff --git a/src/pages/setting/checkUpdate.tsx b/src/pages/setting/checkUpdate.tsx index ea82a030..2a35d21e 100644 --- a/src/pages/setting/checkUpdate.tsx +++ b/src/pages/setting/checkUpdate.tsx @@ -1,10 +1,10 @@ -import intl from 'react-intl-universal'; -import React, { useEffect, useState, useRef, useCallback } from 'react'; -import { Statistic, Modal, Tag, Button, Spin, message } from 'antd'; -import { request } from '@/utils/http'; import config from '@/utils/config'; +import { request } from '@/utils/http'; import WebSocketManager from '@/utils/websocket'; import Ansi from 'ansi-to-react'; +import { Button, Modal, Statistic, message } from 'antd'; +import { useCallback, useEffect, useRef, useState } from 'react'; +import intl from 'react-intl-universal'; const { Countdown } = Statistic; @@ -220,7 +220,7 @@ const CheckUpdate = ({ systemInfo }: any) => {