| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #!/bin/bash
- #
- # DataOps Platform 重启脚本
- # 使用 supervisorctl 重启 gunicorn 服务
- #
- set -e
- # 配置变量
- APP_NAME="dataops-platform"
- APP_DIR="/opt/dataops-platform"
- VENV_DIR="${APP_DIR}/venv"
- LOG_DIR="${APP_DIR}/logs"
- # 颜色输出
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- NC='\033[0m' # No Color
- echo_info() {
- echo -e "${GREEN}[INFO]${NC} $1"
- }
- echo_warn() {
- echo -e "${YELLOW}[WARN]${NC} $1"
- }
- echo_error() {
- echo -e "${RED}[ERROR]${NC} $1"
- }
- # 检查虚拟环境是否存在
- check_venv() {
- if [ ! -d "${VENV_DIR}" ]; then
- echo_error "虚拟环境不存在: ${VENV_DIR}"
- echo_info "请先运行部署脚本创建虚拟环境"
- exit 1
- fi
- }
- # 检查 supervisor 是否运行
- check_supervisor() {
- if ! pgrep -x "supervisord" > /dev/null; then
- echo_warn "supervisord 未运行,正在启动..."
- sudo supervisord -c /etc/supervisor/supervisord.conf
- sleep 2
- fi
- }
- # 重启应用
- restart_app() {
- echo_info "正在重启 ${APP_NAME}..."
-
- sudo supervisorctl restart ${APP_NAME}
-
- # 等待重启
- sleep 3
-
- # 检查状态
- status=$(sudo supervisorctl status ${APP_NAME} | awk '{print $2}')
- if [ "$status" = "RUNNING" ]; then
- echo_info "${APP_NAME} 重启成功!"
- sudo supervisorctl status ${APP_NAME}
- else
- echo_error "${APP_NAME} 重启失败!"
- echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
- exit 1
- fi
- }
- # 健康检查
- health_check() {
- echo_info "正在进行健康检查..."
-
- local max_retries=5
- local retry_interval=3
- local retry_count=0
- local response=""
- local APP_PORT=5500
-
- while [ $retry_count -lt $max_retries ]; do
- sleep $retry_interval
- retry_count=$((retry_count + 1))
-
- # 尝试健康检查接口(使用应用实际端口 5500)
- response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${APP_PORT}/api/system/health 2>/dev/null || echo "000")
-
- if [ "$response" = "200" ]; then
- echo_info "健康检查通过! HTTP 状态码: ${response}"
- return 0
- fi
-
- echo_info "尝试 ${retry_count}/${max_retries}: HTTP 状态码 ${response},等待重试..."
- done
-
- # 如果 /api/system/health 失败,尝试其他接口作为备选
- echo_warn "健康检查接口返回状态码: ${response}"
-
- # 尝试检查 /api/bd/list 接口作为备选(使用 POST 方法)
- response=$(curl -s -o /dev/null -w "%{http_code}" -X POST http://127.0.0.1:${APP_PORT}/api/bd/list -H "Content-Type: application/json" -d "{}" 2>/dev/null || echo "000")
- if [ "$response" = "200" ] || [ "$response" = "500" ]; then
- echo_info "备选接口 /api/bd/list 有响应(${response}),服务已启动!"
- return 0
- fi
-
- echo_warn "服务可能需要更多时间启动,或健康检查接口配置有问题"
- echo_info "请手动检查: curl http://127.0.0.1:${APP_PORT}/api/system/health"
- }
- # 主函数
- main() {
- echo "=========================================="
- echo " DataOps Platform 重启脚本"
- echo "=========================================="
-
- check_venv
- check_supervisor
- restart_app
- health_check
-
- echo ""
- echo_info "重启完成!"
- echo_info "访问地址: http://localhost:5500"
- echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
- }
- main "$@"
|