fix: MySQL 空闲连接健康检查 + Gunicorn worker 保活,解决广播开才不卡

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-09 04:49:08 +08:00
parent 9e61161a5c
commit c4244ad423
4 changed files with 30 additions and 0 deletions

View File

@@ -126,6 +126,7 @@ DATABASES = {
'PASSWORD': app_secrets.DATABASE_PASSWORD, 'PASSWORD': app_secrets.DATABASE_PASSWORD,
'PORT': app_secrets.DATABASE_PORT, 'PORT': app_secrets.DATABASE_PORT,
'CONN_MAX_AGE': 600, 'CONN_MAX_AGE': 600,
'CONN_HEALTH_CHECKS': True,
'OPTIONS': { 'OPTIONS': {
'charset': 'utf8mb4', 'charset': 'utf8mb4',
'init_command': "SET time_zone='+08:00'", 'init_command': "SET time_zone='+08:00'",

View File

@@ -16,6 +16,14 @@ sleep 2
systemctl is-active gunicorn-dianjing.service systemctl is-active gunicorn-dianjing.service
pgrep -cf 'gunicorn.*a_long_dianjing' || true 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 ""
echo ">>> 最近慢请求 (rt>=0.5s):" 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 grep -a 'hqhd' "$NGINX_LOG" | awk 'match($0,/rt=([0-9.]+)/,a){if(a[1]+0>=0.5)print}' | tail -5

View File

@@ -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

16
deploy/gunicorn-warmup.sh Normal file
View File

@@ -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