增加 pm2 平滑重启

This commit is contained in:
whyour 2023-05-06 00:09:03 +08:00
parent 3d11210aeb
commit 5baa8e4391
8 changed files with 54 additions and 45 deletions

View File

@ -18,6 +18,7 @@ async function startServer() {
const server = app const server = app
.listen(config.port, () => { .listen(config.port, () => {
Logger.debug(`✌️ 后端服务启动成功!`); Logger.debug(`✌️ 后端服务启动成功!`);
process.send?.('ready');
}) })
.on('error', (err) => { .on('error', (err) => {
Logger.error(err); Logger.error(err);

View File

@ -9,8 +9,14 @@ export default async ({ server }: { server: Server }) => {
process.on('SIGINT', () => { process.on('SIGINT', () => {
Logger.info('✌️ Server need close'); Logger.info('✌️ Server need close');
server.close(() => { server.close(() => {
Logger.info('✌️ Server closed'); setTimeout(() => {
process.exit(0); process.exit();
}, 10000);
}); });
setTimeout(() => {
console.log('Forcing server close !!!');
process.exit(1);
}, 15000);
}); });
}; };

View File

@ -25,6 +25,7 @@ app
await require('./loaders/db').default(); await require('./loaders/db').default();
Logger.debug(`✌️ 公共服务启动成功!`); Logger.debug(`✌️ 公共服务启动成功!`);
process.send?.('ready');
}) })
.on('error', (err) => { .on('error', (err) => {
Logger.error(err); Logger.error(err);

View File

@ -16,5 +16,6 @@ server.bindAsync(
() => { () => {
server.start(); server.start();
Logger.debug(`✌️ 定时服务启动成功!`); Logger.debug(`✌️ 定时服务启动成功!`);
process.send?.('ready');
}, },
); );

View File

@ -12,7 +12,6 @@ make_dir /run/nginx
init_nginx init_nginx
pm2 l &>/dev/null pm2 l &>/dev/null
pm2 flush &>/dev/null
echo -e "======================2. 安装依赖========================\n" echo -e "======================2. 安装依赖========================\n"
patch_version patch_version
@ -32,36 +31,17 @@ echo -e "======================3. 启动nginx========================\n"
nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf
echo -e "nginx启动成功...\n" echo -e "nginx启动成功...\n"
echo -e "======================4. 启动定时服务========================\n" echo -e "======================4. 启动pm2服务========================\n"
pm2 delete schedule &>/dev/null reload_pm2
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
if [[ $EnableExtraShell == true ]]; then if [[ $EnableExtraShell == true ]]; then
echo -e "======================8. 执行自定义脚本========================\n" echo -e "====================5. 执行自定义脚本========================\n"
nohup ql -l extra >$dir_log/extra.log 2>&1 & nohup ql -l extra >$dir_log/extra.log 2>&1 &
echo -e "自定义脚本后台执行中...\n" echo -e "自定义脚本后台执行中...\n"
fi fi
echo -e "############################################################\n" echo -e "############################################################\n"
echo -e "容器启动成功..." echo -e "容器启动成功..."
echo -e "\n请先访问5700端口登录成功面板之后再执行添加定时任务..."
echo -e "############################################################\n" echo -e "############################################################\n"
crond -f >/dev/null crond -f >/dev/null

31
ecosystem.config.js Normal file
View File

@ -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',
},
],
};

View File

@ -74,12 +74,6 @@ check_pm2() {
fi 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() { main() {
echo -e "=====> 开始检测" echo -e "=====> 开始检测"
npm i -g pnpm@8.3.1 npm i -g pnpm@8.3.1
@ -97,7 +91,6 @@ main() {
pnpm add -g pm2 tsx pnpm add -g pm2 tsx
reset_env reset_env
start_public
copy_dep copy_dep
check_ql check_ql
check_nginx check_nginx

View File

@ -18,6 +18,7 @@ dir_update_log=$dir_log/update
ql_static_repo=$dir_repo/static ql_static_repo=$dir_repo/static
## 文件 ## 文件
file_ecosystem_js=$dir_root/ecosystem.config.js
file_config_sample=$dir_sample/config.sample.sh file_config_sample=$dir_sample/config.sample.sh
file_env=$dir_config/env.sh file_env=$dir_config/env.sh
file_sharecode=$dir_config/sharecode.sh file_sharecode=$dir_config/sharecode.sh
@ -316,6 +317,8 @@ git_pull_scripts() {
echo -e "开始更新仓库:$dir_work" echo -e "开始更新仓库:$dir_work"
local pre_commit_id=$(git rev-parse --short HEAD) local pre_commit_id=$(git rev-parse --short HEAD)
reset_branch "$branch"
set_proxy "$proxy" set_proxy "$proxy"
git fetch --depth=1 --all git fetch --depth=1 --all
git pull --depth=1 &>/dev/null git pull --depth=1 &>/dev/null
@ -344,7 +347,7 @@ reset_romote_url() {
git init git init
git remote add origin $url &>/dev/null git remote add origin $url &>/dev/null
fi fi
cd $dir_current cd $dir_current
} }
@ -353,10 +356,10 @@ reset_branch() {
local part_cmd="HEAD" local part_cmd="HEAD"
if [[ $branch ]]; then if [[ $branch ]]; then
part_cmd="origin/${branch}" part_cmd="origin/${branch}"
git checkout -B "$branch" &>/dev/null git remote set-branches --add origin $branch &>/dev/null
git branch --set-upstream-to=$part_cmd $branch &>/dev/null
fi fi
git reset --hard $part_cmd &>/dev/null git reset --hard $part_cmd &>/dev/null
git checkout --track $part_cmd &>/dev/null
} }
random_range() { random_range() {
@ -366,16 +369,9 @@ random_range() {
} }
reload_pm2() { reload_pm2() {
pm2 l &>/dev/null cd $dir_root
pm2 flush &>/dev/null pm2 flush &>/dev/null
pm2 startOrGracefulReload $file_ecosystem_js
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
} }
diff_time() { diff_time() {
@ -465,7 +461,7 @@ init_nginx() {
cp -fv $nginx_conf /etc/nginx/nginx.conf cp -fv $nginx_conf /etc/nginx/nginx.conf
cp -fv $nginx_app_conf /etc/nginx/conf.d/front.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 sed -i "s,QL_BASE_URL,${qlBaseUrl},g" /etc/nginx/conf.d/front.conf
ipv6=$(ip a | grep inet6) ipv6=$(ip a | grep inet6)
ipv6Str="" ipv6Str=""
if [[ $ipv6 ]]; then if [[ $ipv6 ]]; then