#!/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 } # 启动应用 start_app() { echo_info "正在启动 ${APP_NAME}..." # 检查应用状态 status=$(sudo supervisorctl status ${APP_NAME} 2>/dev/null | awk '{print $2}' || echo "UNKNOWN") if [ "$status" = "RUNNING" ]; then echo_warn "${APP_NAME} 已经在运行中" sudo supervisorctl status ${APP_NAME} return 0 fi # 启动应用 sudo supervisorctl start ${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 start_app health_check echo "" echo_info "启动完成!" echo_info "访问地址: http://localhost:5500" echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log" } main "$@"