restart_dataops.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. #
  3. # DataOps Platform 重启脚本
  4. # 使用 supervisorctl 重启 gunicorn 服务
  5. #
  6. set -e
  7. # 配置变量
  8. APP_NAME="dataops-platform"
  9. APP_DIR="/opt/dataops-platform"
  10. VENV_DIR="${APP_DIR}/venv"
  11. LOG_DIR="${APP_DIR}/logs"
  12. # 颜色输出
  13. RED='\033[0;31m'
  14. GREEN='\033[0;32m'
  15. YELLOW='\033[1;33m'
  16. NC='\033[0m' # No Color
  17. echo_info() {
  18. echo -e "${GREEN}[INFO]${NC} $1"
  19. }
  20. echo_warn() {
  21. echo -e "${YELLOW}[WARN]${NC} $1"
  22. }
  23. echo_error() {
  24. echo -e "${RED}[ERROR]${NC} $1"
  25. }
  26. # 检查虚拟环境是否存在
  27. check_venv() {
  28. if [ ! -d "${VENV_DIR}" ]; then
  29. echo_error "虚拟环境不存在: ${VENV_DIR}"
  30. echo_info "请先运行部署脚本创建虚拟环境"
  31. exit 1
  32. fi
  33. }
  34. # 检查 supervisor 是否运行
  35. check_supervisor() {
  36. if ! pgrep -x "supervisord" > /dev/null; then
  37. echo_warn "supervisord 未运行,正在启动..."
  38. sudo supervisord -c /etc/supervisor/supervisord.conf
  39. sleep 2
  40. fi
  41. }
  42. # 重启应用
  43. restart_app() {
  44. echo_info "正在重启 ${APP_NAME}..."
  45. sudo supervisorctl restart ${APP_NAME}
  46. # 等待重启
  47. sleep 3
  48. # 检查状态
  49. status=$(sudo supervisorctl status ${APP_NAME} | awk '{print $2}')
  50. if [ "$status" = "RUNNING" ]; then
  51. echo_info "${APP_NAME} 重启成功!"
  52. sudo supervisorctl status ${APP_NAME}
  53. else
  54. echo_error "${APP_NAME} 重启失败!"
  55. echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
  56. exit 1
  57. fi
  58. }
  59. # 健康检查
  60. health_check() {
  61. echo_info "正在进行健康检查..."
  62. local max_retries=5
  63. local retry_interval=3
  64. local retry_count=0
  65. local response=""
  66. local APP_PORT=5500
  67. while [ $retry_count -lt $max_retries ]; do
  68. sleep $retry_interval
  69. retry_count=$((retry_count + 1))
  70. # 尝试健康检查接口(使用应用实际端口 5500)
  71. response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:${APP_PORT}/api/system/health 2>/dev/null || echo "000")
  72. if [ "$response" = "200" ]; then
  73. echo_info "健康检查通过! HTTP 状态码: ${response}"
  74. return 0
  75. fi
  76. echo_info "尝试 ${retry_count}/${max_retries}: HTTP 状态码 ${response},等待重试..."
  77. done
  78. # 如果 /api/system/health 失败,尝试其他接口作为备选
  79. echo_warn "健康检查接口返回状态码: ${response}"
  80. # 尝试检查 /api/bd/list 接口作为备选(使用 POST 方法)
  81. 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")
  82. if [ "$response" = "200" ] || [ "$response" = "500" ]; then
  83. echo_info "备选接口 /api/bd/list 有响应(${response}),服务已启动!"
  84. return 0
  85. fi
  86. echo_warn "服务可能需要更多时间启动,或健康检查接口配置有问题"
  87. echo_info "请手动检查: curl http://127.0.0.1:${APP_PORT}/api/system/health"
  88. }
  89. # 主函数
  90. main() {
  91. echo "=========================================="
  92. echo " DataOps Platform 重启脚本"
  93. echo "=========================================="
  94. check_venv
  95. check_supervisor
  96. restart_app
  97. health_check
  98. echo ""
  99. echo_info "重启完成!"
  100. echo_info "访问地址: http://localhost:5500"
  101. echo_info "查看日志: tail -f ${LOG_DIR}/gunicorn_error.log"
  102. }
  103. main "$@"