fix(部署脚本): 增强后端部署脚本的健壮性和调试信息

添加路径检查和进程验证逻辑,改进错误处理和调试日志输出
This commit is contained in:
2025-09-24 00:27:28 +08:00
parent 8c06849254
commit adcd49a5db

View File

@ -9,13 +9,34 @@
set -euo pipefail set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)" # 获取脚本所在目录的绝对路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BACKEND_DIR="$ROOT_DIR/backend" BACKEND_DIR="$ROOT_DIR/backend"
BIN_PATH="$BACKEND_DIR/target/release/udmin" BIN_PATH="$BACKEND_DIR/target/release/udmin"
LOG_FILE="$BACKEND_DIR/udmin.log" LOG_FILE="$BACKEND_DIR/udmin.log"
PID_FILE="$BACKEND_DIR/udmin.pid" PID_FILE="$BACKEND_DIR/udmin.pid"
ENV_FILE_VALUE="${ENV_FILE:-prod}" ENV_FILE_VALUE="${ENV_FILE:-prod}"
# 调试信息:打印关键路径
echo "[backend] Script directory: $SCRIPT_DIR"
echo "[backend] Root directory: $ROOT_DIR"
echo "[backend] Backend directory: $BACKEND_DIR"
echo "[backend] Binary path: $BIN_PATH"
# 检查关键目录是否存在
if [ ! -d "$BACKEND_DIR" ]; then
echo "[backend] ERROR: Backend directory does not exist: $BACKEND_DIR" >&2
echo "[backend] Please ensure the script is placed in the correct location relative to the project structure" >&2
exit 1
fi
if [ ! -f "$BACKEND_DIR/Cargo.toml" ]; then
echo "[backend] ERROR: Cargo.toml not found in backend directory: $BACKEND_DIR" >&2
echo "[backend] This doesn't appear to be a valid Rust project directory" >&2
exit 1
fi
cd "$BACKEND_DIR" cd "$BACKEND_DIR"
echo "[backend] Building release binary..." echo "[backend] Building release binary..."
@ -47,21 +68,32 @@ stop_pid() {
} }
# 1) Stop by PID file if present # 1) Stop by PID file if present
echo "[backend] Checking for existing PID file: $PID_FILE"
if [ -f "$PID_FILE" ]; then if [ -f "$PID_FILE" ]; then
PID_CONTENT="$(cat "$PID_FILE" 2>/dev/null || true)" PID_CONTENT="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "$PID_CONTENT" ]; then if [ -n "$PID_CONTENT" ]; then
echo "[backend] Found PID file with PID: $PID_CONTENT"
stop_pid "$PID_CONTENT" stop_pid "$PID_CONTENT"
else
echo "[backend] PID file exists but is empty"
fi fi
rm -f "$PID_FILE" rm -f "$PID_FILE"
echo "[backend] Removed PID file"
else
echo "[backend] No PID file found"
fi fi
# 2) Fallback: stop any process matching the release binary path (safe, avoids killing debug run) # 2) Fallback: stop any process matching the release binary path (safe, avoids killing debug run)
echo "[backend] Searching for existing processes matching: $BIN_PATH"
PGREP_PIDS="$(pgrep -f "$BIN_PATH" 2>/dev/null || true)" PGREP_PIDS="$(pgrep -f "$BIN_PATH" 2>/dev/null || true)"
if [ -n "$PGREP_PIDS" ]; then if [ -n "$PGREP_PIDS" ]; then
echo "[backend] Found existing udmin by path, stopping: $PGREP_PIDS" echo "[backend] Found existing udmin processes by path: $PGREP_PIDS"
for p in $PGREP_PIDS; do for p in $PGREP_PIDS; do
echo "[backend] Stopping process: $p"
stop_pid "$p" stop_pid "$p"
done done
else
echo "[backend] No existing processes found matching the binary path"
fi fi
# Rotate log # Rotate log
@ -77,6 +109,23 @@ NEW_PID=$!
echo "$NEW_PID" > "$PID_FILE" echo "$NEW_PID" > "$PID_FILE"
echo "[backend] Started. PID=$NEW_PID" echo "[backend] Started with PID: $NEW_PID"
echo "[backend] Logs: $LOG_FILE" echo "[backend] Logs: $LOG_FILE"
echo "[backend] PID file: $PID_FILE" echo "[backend] PID file: $PID_FILE"
# 等待几秒钟验证进程是否成功启动
echo "[backend] Waiting 3 seconds to verify startup..."
sleep 3
if ps -p "$NEW_PID" > /dev/null 2>&1; then
echo "[backend] ✅ Process is running successfully (PID: $NEW_PID)"
echo "[backend] Deployment completed successfully!"
else
echo "[backend] ❌ ERROR: Process failed to start or crashed immediately" >&2
echo "[backend] Check the log file for details: $LOG_FILE" >&2
if [ -f "$LOG_FILE" ]; then
echo "[backend] Last few lines of log:" >&2
tail -10 "$LOG_FILE" >&2
fi
exit 1
fi