From 4cc2ee853d2ab5b16388ec95e38a7ac7f1fc1ec2 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Mon, 10 Aug 2026 12:48:11 +0800 Subject: [PATCH] fix: add graceful shutdown to CI deploy script - scripts/ci_staging_deploy.sh: replace docker rm -f with docker stop + rm Worker: 300s, API: 30s, Web: 10s - infra/docker/compose.yml: add stop_grace_period and stop_signal for api (30s) and worker (300s) - infra/docker/entrypoint-worker.sh: add -W (warm shutdown) flag so Celery waits for current tasks before exiting on SIGTERM Rollback function docker rm -f intentionally kept (speed priority) --- infra/docker/compose.yml | 4 ++++ infra/docker/entrypoint-worker.sh | 3 ++- scripts/ci_staging_deploy.sh | 9 ++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) mode change 100644 => 100755 infra/docker/compose.yml mode change 100644 => 100755 infra/docker/entrypoint-worker.sh diff --git a/infra/docker/compose.yml b/infra/docker/compose.yml old mode 100644 new mode 100755 index b9e7178f5..5c7164c7e --- a/infra/docker/compose.yml +++ b/infra/docker/compose.yml @@ -47,6 +47,8 @@ services: container_name: xiaoxia-api-${ENV:-staging} restart: unless-stopped + stop_grace_period: 30s + stop_signal: SIGTERM # 环境变量文件(包含数据库密码等敏感信息) env_file: @@ -102,6 +104,8 @@ services: container_name: xiaoxia-worker-${ENV:-staging} restart: unless-stopped + stop_grace_period: 300s + stop_signal: SIGTERM env_file: - ../../.env diff --git a/infra/docker/entrypoint-worker.sh b/infra/docker/entrypoint-worker.sh old mode 100644 new mode 100755 index d03eb4bd1..9bbb6f898 --- a/infra/docker/entrypoint-worker.sh +++ b/infra/docker/entrypoint-worker.sh @@ -10,4 +10,5 @@ exec celery \ -A worker_app.celery_app \ worker \ --loglevel=info \ - "--concurrency=${CONCURRENCY}" + "--concurrency=${CONCURRENCY}" \ + -W diff --git a/scripts/ci_staging_deploy.sh b/scripts/ci_staging_deploy.sh index 71444545f..22a34a502 100755 --- a/scripts/ci_staging_deploy.sh +++ b/scripts/ci_staging_deploy.sh @@ -311,9 +311,12 @@ else fi echo "Stopping old containers..." -docker rm -f xiaoxia-api-staging 2>/dev/null || true -docker rm -f xiaoxia-worker-staging 2>/dev/null || true -docker rm -f xiaoxia-web-staging 2>/dev/null || true +# 优雅关闭:先 stop(发 SIGTERM,等待),再 rm +# Worker 需要更长时间(视频任务最长可能5分钟) +docker stop -t 300 xiaoxia-worker-staging 2>/dev/null || true +docker stop -t 30 xiaoxia-api-staging 2>/dev/null || true +docker stop -t 10 xiaoxia-web-staging 2>/dev/null || true +docker rm xiaoxia-worker-staging xiaoxia-api-staging xiaoxia-web-staging 2>/dev/null || true LOG_OPTS="--log-driver json-file --log-opt max-size=50m --log-opt max-file=3"