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