From 5baa8e43913580bcb8081fd2a8eccb2f05055723 Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 6 May 2023 00:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20pm2=20=E5=B9=B3=E6=BB=91?= =?UTF-8?q?=E9=87=8D=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/app.ts | 1 + back/loaders/server.ts | 10 ++++++++-- back/public.ts | 1 + back/schedule/index.ts | 1 + docker/docker-entrypoint.sh | 26 +++----------------------- ecosystem.config.js | 31 +++++++++++++++++++++++++++++++ shell/check.sh | 7 ------- shell/share.sh | 22 +++++++++------------- 8 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 ecosystem.config.js diff --git a/back/app.ts b/back/app.ts index 374ce7e2..a6ae89d0 100644 --- a/back/app.ts +++ b/back/app.ts @@ -18,6 +18,7 @@ async function startServer() { const server = app .listen(config.port, () => { Logger.debug(`✌️ 后端服务启动成功!`); + process.send?.('ready'); }) .on('error', (err) => { Logger.error(err); diff --git a/back/loaders/server.ts b/back/loaders/server.ts index 3d5afefd..6f92b519 100644 --- a/back/loaders/server.ts +++ b/back/loaders/server.ts @@ -9,8 +9,14 @@ export default async ({ server }: { server: Server }) => { process.on('SIGINT', () => { Logger.info('✌️ Server need close'); server.close(() => { - Logger.info('✌️ Server closed'); - process.exit(0); + setTimeout(() => { + process.exit(); + }, 10000); }); + + setTimeout(() => { + console.log('Forcing server close !!!'); + process.exit(1); + }, 15000); }); }; diff --git a/back/public.ts b/back/public.ts index ea04986c..35f326e0 100644 --- a/back/public.ts +++ b/back/public.ts @@ -25,6 +25,7 @@ app await require('./loaders/db').default(); Logger.debug(`✌️ 公共服务启动成功!`); + process.send?.('ready'); }) .on('error', (err) => { Logger.error(err); diff --git a/back/schedule/index.ts b/back/schedule/index.ts index cb4e4e82..98065879 100644 --- a/back/schedule/index.ts +++ b/back/schedule/index.ts @@ -16,5 +16,6 @@ server.bindAsync( () => { server.start(); Logger.debug(`✌️ 定时服务启动成功!`); + process.send?.('ready'); }, ); diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index b84a1d07..98993442 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -12,7 +12,6 @@ make_dir /run/nginx init_nginx pm2 l &>/dev/null -pm2 flush &>/dev/null echo -e "======================2. 安装依赖========================\n" patch_version @@ -32,36 +31,17 @@ echo -e "======================3. 启动nginx========================\n" nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf echo -e "nginx启动成功...\n" -echo -e "======================4. 启动定时服务========================\n" -pm2 delete schedule &>/dev/null -pm2 start $dir_static/build/schedule/index.js -n schedule --source-map-support --time -echo -e "定时任务启动成功...\n" - -echo -e "======================5. 启动面板监控========================\n" -pm2 delete public &>/dev/null -pm2 start $dir_static/build/public.js -n public --source-map-support --time -echo -e "监控服务启动成功...\n" - -echo -e "======================6. 启动主服务========================\n" -pm2 delete panel &>/dev/null -pm2 start $dir_static/build/app.js -n panel --source-map-support --time -echo -e "主服务启动成功...\n" - -if [[ $AutoStartBot == true ]]; then - echo -e "======================7. 启动bot========================\n" - nohup ql -l bot >$dir_log/bot.log 2>&1 & - echo -e "bot后台启动中...\n" -fi +echo -e "======================4. 启动pm2服务========================\n" +reload_pm2 if [[ $EnableExtraShell == true ]]; then - echo -e "======================8. 执行自定义脚本========================\n" + echo -e "====================5. 执行自定义脚本========================\n" nohup ql -l extra >$dir_log/extra.log 2>&1 & echo -e "自定义脚本后台执行中...\n" fi echo -e "############################################################\n" echo -e "容器启动成功..." -echo -e "\n请先访问5700端口,登录成功面板之后再执行添加定时任务..." echo -e "############################################################\n" crond -f >/dev/null diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 00000000..28b47515 --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,31 @@ +module.exports = { + apps: [ + { + name: 'schedule', + max_restarts: 10, + kill_timeout: 15000, + wait_ready: true, + source_map_support: true, + time: true, + script: 'static/build/schedule/index.js', + }, + { + name: 'public', + max_restarts: 10, + kill_timeout: 15000, + wait_ready: true, + source_map_support: true, + time: true, + script: 'static/build/public.js', + }, + { + name: 'panel', + max_restarts: 10, + kill_timeout: 15000, + wait_ready: true, + source_map_support: true, + time: true, + script: 'static/build/app.js', + }, + ], +}; diff --git a/shell/check.sh b/shell/check.sh index 763e31b1..7d26c8c0 100755 --- a/shell/check.sh +++ b/shell/check.sh @@ -74,12 +74,6 @@ check_pm2() { fi } -start_public() { - echo -e "=====> 启动公开服务\n" - pm2 delete public --source-map-support --time &>/dev/null - pm2 start $dir_static/build/public.js -n public --source-map-support --time &>/dev/null -} - main() { echo -e "=====> 开始检测" npm i -g pnpm@8.3.1 @@ -97,7 +91,6 @@ main() { pnpm add -g pm2 tsx reset_env - start_public copy_dep check_ql check_nginx diff --git a/shell/share.sh b/shell/share.sh index b23124e9..29e3f84a 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -18,6 +18,7 @@ dir_update_log=$dir_log/update ql_static_repo=$dir_repo/static ## 文件 +file_ecosystem_js=$dir_root/ecosystem.config.js file_config_sample=$dir_sample/config.sample.sh file_env=$dir_config/env.sh file_sharecode=$dir_config/sharecode.sh @@ -316,6 +317,8 @@ git_pull_scripts() { echo -e "开始更新仓库:$dir_work" local pre_commit_id=$(git rev-parse --short HEAD) + reset_branch "$branch" + set_proxy "$proxy" git fetch --depth=1 --all git pull --depth=1 &>/dev/null @@ -344,7 +347,7 @@ reset_romote_url() { git init git remote add origin $url &>/dev/null fi - + cd $dir_current } @@ -353,10 +356,10 @@ reset_branch() { local part_cmd="HEAD" if [[ $branch ]]; then part_cmd="origin/${branch}" - git checkout -B "$branch" &>/dev/null - git branch --set-upstream-to=$part_cmd $branch &>/dev/null + git remote set-branches --add origin $branch &>/dev/null fi git reset --hard $part_cmd &>/dev/null + git checkout --track $part_cmd &>/dev/null } random_range() { @@ -366,16 +369,9 @@ random_range() { } reload_pm2() { - pm2 l &>/dev/null + cd $dir_root pm2 flush &>/dev/null - - echo -e "启动面板服务\n" - pm2 delete panel --source-map-support --time &>/dev/null - pm2 start $dir_static/build/app.js -n panel --source-map-support --time &>/dev/null - - echo -e "启动定时服务\n" - pm2 delete schedule --source-map-support --time &>/dev/null - pm2 start $dir_static/build/schedule/index.js -n schedule --source-map-support --time &>/dev/null + pm2 startOrGracefulReload $file_ecosystem_js } diff_time() { @@ -465,7 +461,7 @@ init_nginx() { cp -fv $nginx_conf /etc/nginx/nginx.conf cp -fv $nginx_app_conf /etc/nginx/conf.d/front.conf sed -i "s,QL_BASE_URL,${qlBaseUrl},g" /etc/nginx/conf.d/front.conf - + ipv6=$(ip a | grep inet6) ipv6Str="" if [[ $ipv6 ]]; then