From c4244ad423b71cf93c4a3a0147b95dae647d5325 Mon Sep 17 00:00:00 2001 From: XingQue Date: Thu, 9 Jul 2026 04:49:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20MySQL=20=E7=A9=BA=E9=97=B2=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=81=A5=E5=BA=B7=E6=A3=80=E6=9F=A5=20+=20Gunicorn=20?= =?UTF-8?q?worker=20=E4=BF=9D=E6=B4=BB=EF=BC=8C=E8=A7=A3=E5=86=B3=E5=B9=BF?= =?UTF-8?q?=E6=92=AD=E5=BC=80=E6=89=8D=E4=B8=8D=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- a_long_dianjing/settings.py | 1 + deploy/apply-lag-fix.sh | 8 ++++++++ deploy/gunicorn-warmup.cron | 5 +++++ deploy/gunicorn-warmup.sh | 16 ++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 deploy/gunicorn-warmup.cron create mode 100644 deploy/gunicorn-warmup.sh diff --git a/a_long_dianjing/settings.py b/a_long_dianjing/settings.py index fa7ea50..4bd6e5e 100644 --- a/a_long_dianjing/settings.py +++ b/a_long_dianjing/settings.py @@ -126,6 +126,7 @@ DATABASES = { 'PASSWORD': app_secrets.DATABASE_PASSWORD, 'PORT': app_secrets.DATABASE_PORT, 'CONN_MAX_AGE': 600, + 'CONN_HEALTH_CHECKS': True, 'OPTIONS': { 'charset': 'utf8mb4', 'init_command': "SET time_zone='+08:00'", diff --git a/deploy/apply-lag-fix.sh b/deploy/apply-lag-fix.sh index 065c195..f63c856 100644 --- a/deploy/apply-lag-fix.sh +++ b/deploy/apply-lag-fix.sh @@ -16,6 +16,14 @@ sleep 2 systemctl is-active gunicorn-dianjing.service pgrep -cf 'gunicorn.*a_long_dianjing' || true +echo "" +echo ">>> 安装 worker 保活 cron(空闲后不卡)" +chmod +x deploy/gunicorn-warmup.sh +cp deploy/gunicorn-warmup.cron /etc/cron.d/gunicorn-warmup +chmod 644 /etc/cron.d/gunicorn-warmup +bash deploy/gunicorn-warmup.sh +echo " cron 已安装,每2分钟预热 worker" + echo "" echo ">>> 最近慢请求 (rt>=0.5s):" grep -a 'hqhd' "$NGINX_LOG" | awk 'match($0,/rt=([0-9.]+)/,a){if(a[1]+0>=0.5)print}' | tail -5 diff --git a/deploy/gunicorn-warmup.cron b/deploy/gunicorn-warmup.cron new file mode 100644 index 0000000..aba328b --- /dev/null +++ b/deploy/gunicorn-warmup.cron @@ -0,0 +1,5 @@ +# 每 2 分钟预热 Gunicorn worker,解决「空闲后第一下卡、有广播/并发反而不卡」 +SHELL=/bin/bash +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin + +*/2 * * * * root /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django/deploy/gunicorn-warmup.sh >/dev/null 2>&1 diff --git a/deploy/gunicorn-warmup.sh b/deploy/gunicorn-warmup.sh new file mode 100644 index 0000000..fd155cb --- /dev/null +++ b/deploy/gunicorn-warmup.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# 每 2 分钟 ping 一次 Gunicorn,避免 worker/数据库连接空闲后「第一下卡」 +# 安装: cp deploy/gunicorn-warmup.cron /etc/cron.d/gunicorn-warmup && chmod 644 /etc/cron.d/gunicorn-warmup + +DJANGO_DIR="/opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django" +cd "$DJANGO_DIR" + +# 6 个 worker 各打一次,保持连接池温热 +for _ in 1 2 3 4 5 6; do + curl -s -o /dev/null --max-time 3 http://127.0.0.1:8001/ & +done +wait + +# 顺带测一条需连库的轻量配置接口(无需 token 的会 401/404,但会走 Django+MySQL) +curl -s -o /dev/null --max-time 5 -X POST http://127.0.0.1:8001/hqhd/peizhi/qdpzhq \ + -H 'Content-Type: application/json' -d '{}' 2>/dev/null || true